Forum Moderators: coopster

Message Too Old, No Replies

Help with PHP list sorting

         

max_naylor

12:13 pm on Nov 6, 2008 (gmt 0)

10+ Year Member



Hello...

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\"/>&nbsp;&nbsp;" .
"{$row['event_name']}</div>" .
"<div id=\"eventlocation\">{$row['event_location']}</div>" .
"<div id=\"eventlink\"><a href=\"{$row['event_link']}\">Website &raquo;</a></div></div>";
}

include 'closedb.php';
?>

supermanjnk

8:42 pm on Nov 6, 2008 (gmt 0)

10+ Year Member



you can find the current date by using the date() [us2.php.net] function

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.

max_naylor

10:50 pm on Nov 7, 2008 (gmt 0)

10+ Year Member



Any idea how to codify that exactly?

supermanjnk

8:53 am on Nov 8, 2008 (gmt 0)

10+ Year Member



It's a little rough... it's 4am here and I may not be thinking straight, but something like this should work.

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