我有一个与存储在mySQL数据库中的广告有关的问题。我需要获取所有结果,按id下降限制排序为8,然后在一个< div>中显示4个结果另外4个在另一个< div>。
这是我目前的代码:
<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)
答案 0 :(得分:2)
您可以使用单个查询执行此操作:
$query = "SELECT 'adverts.id', 'adverts.advertiser_id', 'adverts.image' 'advertisers.company_name'
FROM adverts
LEFT JOIN advertisers ON adverts.advertiser_id = advertisers.id
WHERE((expiry_date > NOW()) && (live = 1))
ORDER BY id DESC LIMIT 8";
$count = 0;
while($showAdverts=mysql_fetch_array(mysql_query($query)) {
if ($count < 4){
//display first four
} else {
//display second four
}
$count++;
}
答案 1 :(得分:1)
这是我之前在项目中使用过的一个解决方案。
1)获取所有记录并存储到php数组
$rows = array();
while($row = mysql_fetch_array(YOUR_QUERY))
{
$rows[] = $row;
}
2)现在使用foreach()
循环使用从查询中获取的数据
if(is_array($rows)) {
foreach($rows as $row) {
//do with $row or create some if,switch condition here !
}
}
对于特定限制和调整,我们从mysql_fetch_array()
获得结果集!
答案 2 :(得分:1)
为什么你不只是制作一个if(i <= 4) - &gt;把它写在第一个div或(否则)写在第二个div?