1

我有一个与存储在 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>&nbsp;</p>
          <p>&nbsp;</p>
          <h1>Contact <span class="green"><?php echo $showAdvertiserData['company_name']; ?></span></h1>
          <p>&nbsp;</p>
          <p>&nbsp;</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>&nbsp;</p>
        </div>
    </div>

    <?php $i++; ?>
    <?php } } ?>
 </div>

<div id="second_adverts">
<?php // second set of ads here ?>
</div>

如您所见,这显示了所有 8 个结果,然后检查广告的到期日期是否在未来。基本上我需要上面的代码来显示前 4 个结果,然后在页面后面分别显示最后 4 个结果。(两个查询之间会有更多的 HTML)

4

3 回答 3

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++;
}
于 2013-07-03T09:00:06.953 回答
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()

于 2013-07-03T08:35:51.430 回答
1

为什么你不只是做一个 if (i <= 4) -> 把它写在第一个 div 或者 (else) 把它写在第二个 div 中?

于 2013-07-03T08:45:09.000 回答