Forum Moderators: coopster
I have a PHP set up on my server which takes events from a database and collates them into a nice formatted list, with subdivisions for each month. I’m wondering if it would be possible to add an IF statement to this script which adds two further headings, "past events" and "upcoming events. These headings would be a level above the month headings, so you would have the structure:
* Past events
** September
*** Event X
* Future events
** November
*** Event Y
Basically the script needs to determine the current month and date, and place the headers at appropriate points in the list. My current PHP script is as follows:
<?php
include 'config.php';
include 'opendb.php';
$eventquery = 'SELECT *FROM `Calendar Events` ORDER BY `event_month` , `event_date` , `event_endmonth` , `event_enddate` ASC LIMIT 0 , 100';
$eventresult = mysql_query($eventquery);
$lastPrintedMonth="";
while($row = mysql_fetch_array($eventresult, MYSQL_ASSOC))
{
if($row['event_month']!=$lastPrintedMonth)
{
echo "<div id=\"dateheader\">{$row['event_month']}</div>";
$lastPrintedMonth=$row['event_month'];
}
if ($row['event_important']=="true")
echo "<div id=\"importanteventrow\">";
else
echo "<div id=\"eventrow\">";
echo "<div id=\"eventdate\"><strong>{$row['event_date']}{$row['event_enddate']} {$row['event_endmonth']}</strong></div> <div id=\"eventtitle\"><img src=\"images/flag_{$row['event_flag']}.gif\" align=\"absmiddle\"/> " .
"{$row['event_name']}</div>" .
"<div id=\"eventlocation\">{$row['event_location']}</div>" .
"<div id=\"eventlink\"><a href=\"{$row['event_link']}\">Website »</a></div></div>";
}
include 'closedb.php';
?>
you can then add an if statement that will check the current date vs the date of the current row,
you will need to add a variable as a check to make sure you haven't already printed out the past events and one for the future events text so it's not print out every row.
$past = 0;
$future = 0;
$month = date('m');
$year = date('Y');
if ($row['event_month']!=$lastPrintedMonth) {
if ($row['event_month'] == date('F', mktime(0,0,0,$month,1,$year)) && $past ==0) {
$past =1;
echo "<div>Past Events</div>";
}
if ($row['event_month'] == date('F', mktime(0,0,0,$month + 1,1,$year)) && $future == 0) {
$future =1;
echo "<div>Future Events</div>";
}
echo "<div id=\"dateheader\">{$row['event_month']}</div>";
$lastPrintedMonth=$row['event_month'];
}