Welcome to WebmasterWorld Guest from 34.237.76.91

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

partial calendar script

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

Preferred Member

10+ Year Member

joined:May 20, 2003
posts:493
votes: 0


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

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


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.
4:19 am on Jan 24, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:May 20, 2003
posts:493
votes: 0


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

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members