Forum Moderators: coopster

Message Too Old, No Replies

partial calendar script

         

RammsteinNicCage

1:50 am on Jan 23, 2006 (gmt 0)

10+ Year Member



I have a mysql database with a table that contains 6 elements, one of which is a date in yyyy-mm-dd format. So far, I've managed to get it to select all of the dates for the selected year. My problem is, I would then like to list the month and underneath the month, list certain dates that are in that month (it's a tour calendar). For example:

January
- January 3, 2006
- January 12, 2006

March
- March 8, 2006
- March 29, 2006

December
- December 31, 2006

I do not want to list any months that do not have dates listed with them. I'm still learning php and have been racking my brain for the last two days trying to figure out how to do this, so any help will be appreciated.

Jennifer

coopster

2:38 pm on Jan 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You have your result set then so now all you need to do is process it. Loop the the result set monitoring for any change in months and process accordingly. To make this even easier, you could return just the month portion of the date in your query as it's own column. Something like this:
$sql = "SELECT MONTH(mydate) AS mymonth, mydate FROM table ORDER BY mymonth, mydate"; 
$rows = mysql_query($sql);
$month = false; // initialize
while ($row = mysql_fetch_array($rows)) {
if ($month !== $row['mymonth']) {
print "$mymonth<br />";
}
print "&nbsp;&nbsp;{$row['mydate']}";
}

You will have to do some tweaking here, but it should give you an idea on how to start.

RammsteinNicCage

4:19 am on Jan 24, 2006 (gmt 0)

10+ Year Member



Thanks for the help! There was one part missing from the code you posted, but it made me feel happy that I was able to figure out what it was - makes me feel like I'm actually understanding. ;) The part that was missing was after printing mymonth, I have to set month=mymonth so that it doesn't keep printing the same month.

Jennifer

coopster

2:07 pm on Jan 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep, you're right, I did indeed forget that part. Nice catch and good work.