homepage Welcome to WebmasterWorld Guest from 54.166.105.24
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
grouping calendar results by month
max_naylor




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

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]

 

mooger35




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

what type are the date fields in your mysql database?

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

Anyango




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

<?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]

max_naylor




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

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?

nick279




msg:3775697
 12:29 pm on Oct 29, 2008 (gmt 0)

You can normally go SORT BY `field1`, `field2` and it will sort multiple times

max_naylor




msg:3775791
 2:29 pm on Oct 29, 2008 (gmt 0)

Thanks, this solved the issue.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved