while循环只显示一个结果

时间:2011-09-06 11:10:42

标签: php mysql

有人可以告诉我下面的代码有什么问题吗?由于某种原因,它只返回一个结果。我已经检查过使用mysql_num_rows()从查询中返回了多少行,并且它说了两个,那为什么我只看到一个结果?

function get_events($amount) {
    global $wpdb;
    $today = date('Y-m-d');
    $result = mysql_query(
        "SELECT
            wp_eventscalendar_main.eventTitle, wp_eventscalendar_main.eventDescription, wp_eventscalendar_main.eventStartDate, wp_eventscalendar_main.postID, wp_postmeta.post_id, wp_postmeta.meta_key, wp_postmeta.meta_value 
         FROM
            wp_eventscalendar_main, wp_postmeta
         WHERE
            STR_TO_DATE('$today', '%Y-%m-%d') < wp_eventscalendar_main.eventStartDate AND
            wp_postmeta.post_id = wp_eventscalendar_main.postID AND
            wp_postmeta.meta_key = '_thumbnail_id'
         ORDER BY wp_eventscalendar_main.eventStartDate ASC
         LIMIT 10") or die(mysql_error());

    while($row = mysql_fetch_array($result)) {

     $thumbpostid = $row['meta_value'];

     $result2 = mysql_query("
     SELECT
        post_id, meta_key, meta_value
     FROM
        wp_postmeta
     WHERE
        post_id = $thumbpostid AND
        meta_key = '_wp_attached_file'") or die(mysql_error());

     while($row2 = mysql_fetch_array($result2)) {
        $thumburl = explode(".",$row2['meta_value']); 
     }

     $shortdesc = limitWrds($row['eventDescription'], 10);

     $rawdate = date('d M', strtotime($row['eventStartDate']));
     $date = explode(' ', $rawdate);

     $postlink = strtolower(str_replace(' ', '-', preg_replace("/[^a-zA-Z0-9\s]/", "", $row['eventTitle']))); 

      echo "<div class=\"event\">
                <div class=\"txt\">
                    <h5><a href='/" . $postlink ."'>" . $row['eventTitle'] . "&sbquo;&nbsp;" . $date[1] ."&nbsp;" . $date[0] . "</h5></a>
                    <p>" . $shortdesc ."&hellip;&nbsp;
                    <a href=\"" . $postlink . "\">find out more &raquo;</a></p>
                </div><img src='/wp-content/uploads/". $thumburl[0] ."-42x42.jpg' alt='". $row['eventTitle'] ."' />
            </div>";
            echo _clear('15');    
      }
}

谢谢,

1 个答案:

答案 0 :(得分:0)

您需要将所有代码放入第二个while循环中。你基本上覆盖了第一行。

 while($row2 = mysql_fetch_array($result2)) {
    $thumburl = explode(".",$row2['meta_value']); 
 }

它可能循环两次,但你只有1个变量。

应该是:

 while($row2 = mysql_fetch_array($result2)) {
    $thumburl = explode(".",$row2['meta_value']); 

    $shortdesc = limitWrds($row['eventDescription'], 10);

    $rawdate = date('d M', strtotime($row['eventStartDate']));
    $date = explode(' ', $rawdate);

    $postlink = strtolower(str_replace(' ', '-', preg_replace("/[^a-zA-Z0-9\s]/", "", $row['eventTitle']))); 

    echo "<div class=\"event\">
            <div class=\"txt\">
                <h5><a href='/" . $postlink ."'>" . $row['eventTitle'] . "&sbquo;&nbsp;" . $date[1] ."&nbsp;" . $date[0] . "</h5></a>
                <p>" . $shortdesc ."&hellip;&nbsp;
                <a href=\"" . $postlink . "\">find out more &raquo;</a></p>
            </div><img src='/wp-content/uploads/". $thumburl[0] ."-42x42.jpg' alt='". $row['eventTitle'] ."' />
        </div>";
        echo _clear('15');   
 }

据我所知。