开发者

While loop only displaying one result

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 = m开发者_如何学编程ysql_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,


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜