我有一个与存储在 mySQL 数据库中的广告有关的问题。我需要获取所有结果,按 id 降序排序到 8,然后在一个 <div> 中显示 4 个结果,在另一个 <div> 中显示另外 4 个结果。
这是我当前的代码:
<div id="first_adverts">
<?php $i = 1; ?>
<?php $getAdverts=mysql_query(" SELECT * FROM adverts WHERE live = 1 ORDER BY id DESC LIMIT 8");
while($showAdverts=mysql_fetch_array($getAdverts)) {
$checkdate = $showAdverts['expiry_date']; // Date From Advert in Database
$checkdate = strtotime(str_replace("/","-",$checkdate)); // Change Date format to 01-07-2013 instead of 01/07/2013
if ($checkdate > time()) { // If date is in the future (EG the advert hasn't expired) then show:
$showAdvertiserData=mysql_fetch_array(mysql_query(" SELECT * FROM advertisers WHERE id = '".$showAdverts['advertiser_id']."'")); ?>
<div class="advert-cell">
<a href="#" class="topopup_<?php echo $i; ?>">
<img src="images/adverts/<?php echo $showAdverts['image']; ?>" width="200">
</a>
</div>
<div id="toPopup_<?php echo $i; ?>">
<div class="close"></div>
<div id="popup_content">
<p> </p>
<p> </p>
<h1>Contact <span class="green"><?php echo $showAdvertiserData['company_name']; ?></span></h1>
<p> </p>
<p> </p>
<form id="advertenquiry<?php echo $i; ?>" name="advertenquiry<?php echo $i; ?>" method="post" action="" onSubmit="return validateAdvertEnquiryForm<?php echo $i; ?>()">
<input name="advertiser_id" type="hidden" value="<?php echo $showAdverts['advertiser_id']; ?>">
<input name="advert_id" type="hidden" value="<?php echo $showAdverts['id']; ?>">
<p><input name="enquiry_name" type="text" id="name" value="Name *" onFocus="clearMe(this)" style="width: 250px;" /></p>
<p><input name="enquiry_telephone" type="text" id="telephone" value="Telephone *" onFocus="clearMe(this)" style="width: 250px;" /></p>
<p><input name="enquiry_email" type="text" id="email" value="Email *" onFocus="clearMe(this)" style="width: 250px;" /></p>
<p><textarea name="enquiry_query" id="query" cols="45" rows="5" onFocus="clearMe(this)" style="width: 300px;" >Query </textarea></p>
<p><input type="submit" name="enquire" value="Send" class="submit_button" /></p>
</form>
<p> </p>
</div>
</div>
<?php $i++; ?>
<?php } } ?>
</div>
<div id="second_adverts">
<?php // second set of ads here ?>
</div>
如您所见,这显示了所有 8 个结果,然后检查广告的到期日期是否在未来。基本上我需要上面的代码来显示前 4 个结果,然后在页面后面分别显示最后 4 个结果。(两个查询之间会有更多的 HTML)