开发者

Display same-day events together (PHP/MYSQL)

$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>';

This is a snippet from a plugin that I'm using. It prints event date followed by description. How do I modify it so that all same-day events are printed together as one list?

This is the relevant function in a separate file:

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_optio开发者_Python百科n('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;
    }

Thanks


You can regroup them under their date values.

$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;
}

That will leave you with an array of arrays of events organized 'date' => Array($event, $event, ...).

From there you can work with the entire grouped list:

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

or get a list of events for a specific date:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜