一起显示当天的事件(PHP / MYSQL)

时间:2010-02-13 06:55:59

标签: php mysql wordpress

$sidebarEvent = '<h4 class="scheduler_sidebar_link"><a href="{*URL*}#date={*DATE_SQL*},mode=month" target="_blank">{*DATE*}</a></h4>
                 <div class="scheduler_sidebar_text">{*TEXT*}</div>';

这是我正在使用的插件的片段。它打印事件日期,然后是描述。如何修改它以便将所有当天事件作为一个列表一起打印?

这是单独文件中的相关功能:

for ($i = 0; $i < count($events); $i++) {
        $event = $sidebarEvent;
        $start_date = str_replace("-", "/", $events[$i]->start_date);
        $start_date = date_parse($events[$i]->start_date);
        $start_date = mktime($start_date['hour'], $start_date['minute'], $start_date['second'], $start_date['month'], $start_date['day'], $start_date['year']);
        $start_date = date(get_option('date_format').' '.get_option('time_format'), $start_date);
        $event = str_replace("{*URL*}", $url, $event);
        $event = str_replace("{*DATE*}", $start_date, $event);
        $event = str_replace("{*DATE_SQL*}", $events[$i]->start_date, $event);
        $event = str_replace("{*TEXT*}", $events[$i]->text, $event);
        $final .= $event;
    }

由于

1 个答案:

答案 0 :(得分:1)

您可以在日期值下重新组合它们。

$event_groups = array();
foreach($events as $event) {
  if(!isset($event_groups[$event->start_date]))
    $event_groups[$event->start_date] = array();

  $event_groups[$event->start_date] = $event;
}

这将为您提供一系列由'date' => Array($event, $event, ...)组织的活动数组。

从那里你可以使用整个分组列表:

foreach($event_groups as $date => $events) {
  for ($i = 0; $i < count($events); $i++) {
    $event = $sidebarEvent;
    $start_date = str_replace("-", "/", $events[$i]->start_date);
    $start_date = date_parse($events[$i]->start_date);
    $start_date = mktime($start_date['hour'], $start_date['minute'], $start_date['second'], $start_date['month'], $start_date['day'], $start_date['year']);
    $start_date = date(get_option('date_format').' '.get_option('time_format'), $start_date);
    $event = str_replace("{*URL*}", $url, $event);
    $event = str_replace("{*DATE*}", $start_date, $event);
    $event = str_replace("{*DATE_SQL*}", $events[$i]->start_date, $event);
    $event = str_replace("{*TEXT*}", $events[$i]->text, $event);
    $final .= $event;
  }
}

或获取特定日期的事件列表:

$list = $event_groups['2010-02-13'];