Display a list of the next (x) upcoming events
Last modified: July 25, 2007 - 02:42
PLEASE NOTE! The following snippets are user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.
<?php
/**
* the following displays a list of the 10 upcoming event titles and
* links to their full event nodes. To increase/decrease
* the number of titles displayed..simply change the $listlength value
*
* Works with drupal 4.5.x and drupal 4.6
*/
$listlength="10";
print event_block_upcoming($limit = $listlength);
?>Changing the layout
To have more control over the layout, here is a more complex way to do it:
<?php
function get_list_of_events($limit=10, $show_teaser=false) {
global $user;
// For two hours, we display "NOW"
$time = time() - (2 * 60 * 60);
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.type, n.status, n.moderate, n.teaser, e.event_start FROM {node} n INNER JOIN {event} e USING (nid) WHERE n.status = 1 AND n.moderate = 0 AND e.event_start >= %d ORDER BY event_start"), $time, 0, $limit);
while ($node = db_fetch_object($result)) {
$links[] = $node;
}
if (is_array($links) && (sizeof($links) > 0)) {
foreach ($links as $node) {
$minutesleft = floor(($node->event_start - time()) / 60);
if ($minutesleft < 0) {
$timeleft = t('NOW');
}
else if ($minutesleft < 60) {
$timeleft = format_plural($minutesleft, '1 minute', '%count minutes');
}
else if ($minutesleft >= 60 && $minutesleft < (24 * 60)) {
$timeleft = format_plural(floor($minutesleft / 60), '1 hour', '%count hours');
}
else if ($minutesleft >= (24 * 60)) {
$days = floor($minutesleft / (24 * 60));
// hours remainder
$hours = ($minutesleft % (24 * 60)) / 60;
// hours left in the day
$hours_left = ((time() / 60) % (24 * 60)) / 60;
// see if the remainder of hours on the event date is greater than the hours left in today, if so increase the days by one so that the days remaining mimics the date rather than how many 24 hour periods there are between now and then.
if ($hours>$hours_left) {
$days++;
}
$timeleft = format_plural($days, '1 day', '%count days');
}
$node->timeleft = $timeleft;
$ctype = module_invoke('flexinode', 'load_content_type', substr($node->type, 10));
$node->typename = ($ctype->name ? $ctype->name : $node->type);
$event_line .= "<li>";
$event_line .= "<dl class=\"short_event\"><dt><a href=\"" . print_path($n->nid) ."\">\n";
$event_line .= $node->title;
$event_line .= "</a></dt><dd>" . date("m/d/y", $node->event_start) . "</dd>\n";
if($show_teaser) {
$event_line .= "<dd>" .$node->teaser ."</dd>\n";
}
$event_line .= "<dd><a href=\"" . print_path($n->nid) ."\">Read More</a></dd></dl>";
$event_line .= "</li>";
}
}
return $event_line;
}
?>You can set $teaser to true if you want to get an event description in a listing. Obviously, you'll need to change the display to meet your needs, but if you want get a list of the events by event date, this is a handy way to do it.
