Forum Moderators: coopster
I've managed to put together a little customizable event calendar on a site that just pulls events from a database and displays them. You can check a working version out <snip>
Only problem is that it seems to skip every other event day. In that calendar sample there should be events on Tuesdays and Thursdays but it's not showing up.
Here's the code: The idea is that on any day where there's an event, it displays the info, otherwise it just displays the number. Any help'd be greatly appreciated.
// SPLIT THE MONTH FROM THE YEAR
$indDates = explode("-", $date);
// ASSIGN MONTH AND YEAR TO INDIVDUAL VARIABLES
$month = $indDates[0];
$year = $indDates[1];//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month //falls on
$day_of_week = date('D', $first_day) ;
//Once we know what day of the week it falls on,
//we know how many blank days occure before it.
//If the first day of the week is a Sunday then it would be zero.
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
echo "
<table id=\"calendar\">\n
\t<tr id=\"calHeader\">\n
\t\t<td colspan=\"7\"><strong> $title $year </strong></td>\n
\t</tr>\n
\t<tr id=\"calDays\">\n
\t\t<td>S</td>\n
\t\t<td>M</td>\n
\t\t<td>T</td>\n
\t\t<td>W</td>\n
\t\t<td>T</td>\n
\t\t<td>F</td>\n
\t\t<td>S</td>\n
\t</tr>\n
";
//This counts the days in the week, up to 7
$day_count = 1;
echo "
\t<tr>\n
";
//first we take care of those blank days
while ( $blank > 0 ) {
echo "
\t\t<td></td>\n
";
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month ) {
// QUERY FOR EVENTS
$getEvents_query = "
SELECT
*
FROM
sitehq_events
WHERE
event_year = '$year'
AND
event_month = '$month'
AND
event_day = '$day_num'
";
$getEvents_result = mysql_query($getEvents_query)
or die('Error executing query:<br>'.$getEvents_query.'<br>mySQL said: '.mysql_error());
if (mysql_num_rows($getEvents_result) < 1) { }
while($myrow = mysql_fetch_array($getEvents_result)) {
$event_day = $myrow['event_day'];
$event_month = $myrow['event_month'];
$event_year = $myrow['event_year'];
$event_title = $myrow['event_title'];
$event_info = $myrow['event_info'];
// ADD SLASHES SO JAVASCRIPT'LL WORK
$event_title = addslashes($event_title);
$event_info = addslashes($event_info);
echo "
<td style=\"background-color:#a4d048;color:white;\">$day_num<br><a href=\"#\" onMouseover=\"ddrivetip('".$event_info."')\" onMouseout=\"hideddrivetip()\">$event_title</a></td>\n
";
$day_num++;
$day_count++;
if ($day_count > 7) {
echo "
\t</tr>\n
\t<tr>\n
";
$day_count = 1;
}
} // CLOSES WHILE LOOP FOR EVENT RESULTS
// DISPLAY REGULAR DAYS
echo "
\t\t<td> $day_num </td>\n
";
$day_num++;
$day_count++;
//Make sure we start a new row every week
if ($day_count > 7) {
echo "
\t</tr>\n
\t<tr>\n
";
$day_count = 1;
}
}
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 ) {
echo "
\t\t<td> </td>\n
";
$day_count++;
}
echo "
\t</tr>\n
</table>
";
}
[edited by: dreamcatcher at 7:16 pm (utc) on Sep. 12, 2008]
[edit reason] No urls please! [/edit]
Search engines see infinite duplicate content when they follow the "next" and "previous" links, and spider your calendar for every date hundreds of years in to the past and into to the future.