-3

这是我的 PHP/SQL 请求

<?php $events = $wpdb->get_results( 
    "
    SELECT * 
    FROM  `wp_em_events` 
    LIMIT 5
    // HERE'S MY ORDER BY DATE
    "
);

$dt = new DateTime($event->event_start_date);
$formattedDate = $dt->format('d M Y');

foreach ( $events as $event ) 
{

echo '<div class="home-event">';
    echo $formattedDate;
    echo $event->event_name;
echo '</div>';

}

?>

根据包含列“ event_start_date ”的表,其中值如下所示:

2013-03-27 
2013-04-03
2013-03-31

我想在这些日期之前订购我的活动(首先是离我们最近的)

我试过单

ORDER BY event_start_date DESC

但它“烧毁”了我的请求

-------- 奖金问题 --------

我怎样才能增加我的事件才能拥有:

<div class="home-event-1">
<div class="home-event-2">
<div class="home-event-3">
...

非常感谢你 !!

4

1 回答 1

0

ORDER BY出现在之前LIMIT。_ 你会想要:

SELECT
    * 
FROM
    `wp_em_events` 
ORDER BY
    event_start_date DESC
LIMIT 5

此外,您在错误的位置获取日期。它需要迭代内:

foreach ( $events as $event ) 
{

  $dt = new DateTime($event->event_start_date);

  $formattedDate = $dt->format('d M Y');

  // ...

至于增量:

foreach ( $events as $key => $event ) 
{

    echo '<div class="home-event-' . ($key + 1) . '">';
    echo $formattedDate;
    echo $event->event_name;
    echo '</div>';

}

当然,假设$events不是关联数组。如果是:

$i = 0;

foreach ( $events as $event ) 
{

    echo '<div class="home-event-' . ++$i . '">';
    echo $formattedDate;
    echo $event->event_name;
    echo '</div>';

}
于 2013-03-27T18:33:57.247 回答