Welcome to WebmasterWorld Guest from 54.167.213.22

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

grouping calendar results by month

     
11:11 pm on Oct 28, 2008 (gmt 0)

5+ Year Member



Hello... this is my first post here and I need some help. Iím completely new to PHP and MySQL, but I am fluent in HTML and CSS... i.e. I can make websites look pretty but not make them do very much. I am trying to write a script which will support an events calendar. I have a MySQL database set up on my server, and I have been able to get a basic PHP query up and running which takes events from the database and plonks them out onto a page, into a nice neat CSS table.

This works fine, however my question revolves around how I can get these events to separate by month. I would like the events to be grouped by subheading, i.e. so it says October 2008, and underneath which are all the events for October 2008, then the next subheading November, December etc. The current month needs to be the first heading on display. I canít figure out how to make the events appear underneath each heading, as all my query does at the moment is print out the entire contents of the database into a nice little table. Any help or suggestions as to how I might do this would be greatly appreciated. My exisiting PHP code is below:

<?php
include 'config.php';
include 'opendb.php';

$eventquery = 'SELECT * FROM `Calendar Events` LIMIT 0, 30 ';
$eventresult = mysql_query($eventquery);

while($row = mysql_fetch_array($eventresult, MYSQL_ASSOC))
{
echo "<div id=\"eventrow\"><div id=\"eventdate\"><strong>{$row['event_date']} {$row['event_month']}{$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';
?>

[edited by: eelixduppy at 11:58 pm (utc) on Oct. 28, 2008]
[edit reason] removed URL [/edit]

11:45 pm on Oct 28, 2008 (gmt 0)

10+ Year Member



what type are the date fields in your mysql database?

varchar, timestamp, datetime, date, etc...?

3:53 am on Oct 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
include 'config.php';
include 'opendb.php';

$eventquery = 'SELECT * FROM `Calendar Events` ORDER BY event_month LIMIT 0, 30 ';
$eventresult = mysql_query($eventquery);
$lastPrintedMonth="";
while($row = mysql_fetch_array($eventresult, MYSQL_ASSOC))
{
if($row['event_month']!=$lastPrintedMonth)
{
echo "<br>{$row['event_month']}<br>"; //add all your row formatting here please
$lastPrintedMonth=$row['event_month'];
}

echo "<div id=\"eventrow\"><div id=\"eventdate\"><strong>{$row['event_date']} {$row['event_month']}{$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';
?>

[edited by: Anyango at 3:54 am (utc) on Oct. 29, 2008]

11:37 am on Oct 29, 2008 (gmt 0)

5+ Year Member



Thank you, this is exactly the solution I was looking for! I do have another small question though, is there any way to do multiple sorts? What Iím looking for is a way to have the list sorted by event_month, and then within that the events being sorted by event_date... to keep all of the events in date order within the month headings. Is this doable?
12:29 pm on Oct 29, 2008 (gmt 0)

5+ Year Member



You can normally go SORT BY `field1`, `field2` and it will sort multiple times
2:29 pm on Oct 29, 2008 (gmt 0)

5+ Year Member



Thanks, this solved the issue.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month