doushi5024 2011-09-06 11:10
浏览 103
已采纳

while循环只显示一个结果

Could someone please tell me what is wrong with the below code? For some reason it's only returning one result. I have checked to see how many rows are being returned from the query using mysql_num_rows() and it says two, so why do I only see one result?

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');    
      }
}

Thanks,

  • 写回答

1条回答 默认 最新

  • duangou1953 2011-09-06 11:18
    关注

    You need to put all that code into the second while loop. You are basically overwriting the first row.

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

    It's probably looping twice, but you only have 1 variable.

    Should be:

     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');   
     }
    

    as far as i can tell.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥15 基于OPENCV的人脸识别
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题