Hi,
Could anyone help me with the code below?
I have this nearly working, but I can only get the last element in my event array to be highlighted on the calendar.
Can anbody help, Im very stuck.
Thankyou
<html>
<head>
<title>PHP Calendar</title>
<style type="text/css">
<!--
.calendar {border: 1px solid #000000; border-collapse: collapse; color: #000000; background: #FFFFFF;}
.today {border: 1px solid white; color: #000000; background: #EFEFEF; font-weight: bold;}
.monthdays {border: 1px solid #434470; color: #000000; background: #FFFFFF;}
.nonmonthdays {border: 1px solid white; color: #000000; background: #EFEFEF;}
.eventtoday {border: 1px solid white; color: #000000; background: #7fffd4; font-weight: bold;}
.eventmonthdays {border: 1px solid #434470; color: #000000; background: #00ffff}
-->
</style>
<body>
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "test";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
error_reporting('0');
//ini_set('display_errors', '0');
if (!isset($_REQUEST['date'])){
$date = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
} else {
$date = $_REQUEST['date'];
}
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$month_start = mktime(12, 0, 0, $month, 1, $year);
$month_end = mktime(12, 0, 0, $month + 1, 1, $year);
$month_name = date('M', $month_start);
$month_start_day = date('D', $month_start);
$offset = date('w', $month_start);
if ($month == 1) {
$num_days_last = cal_days_in_month(0, 12, ($year - 1));
} else {
$num_days_last = cal_days_in_month(0, ($month - 1), $year);
}
$num_days_current = cal_days_in_month(0, $month, $year);
for ($i = 1; $i <= $num_days_current; $i++) {
$num_days_array[] = $i;
}
for ($i = 1; $i <= $num_days_last; $i++) {
$num_days_last_array[] = $i;
}
if ($offset > 0) {
$offset_correction = array_slice($num_days_last_array, -$offset, $offset);
$new_count = array_merge($offset_correction, $num_days_array);
$offset_count = count($offset_correction);
} else {
$offset_count = 0;
$new_count = $num_days_array;
}
$current_num = count($new_count);
if ($current_num > 35) {
$num_weeks = 6;
$outset = (42 - $current_num);
} elseif ($current_num < 35) {
$num_weeks = 5;
$outset = (35 - $current_num);
}
if ($current_num == 35) {
$num_weeks = 5;
$outset = 0;
}
for ($i = 1; $i <= $outset; $i++) {
$new_count[] = $i;
}
$weeks = array_chunk($new_count, 7);
$previous_link = '<a href="' . $_SERVER['PHP_SELF'] . '?date=';
if ($month == 1) {
$previous_link .= mktime(0, 0, 0, 12, $day, ($year - 1));
} else {
$previous_link .= mktime(0, 0, 0, ($month - 1), $day, $year);
}
$previous_link .= '"><< Prev</a>';
$next_link = '<a href="' . $_SERVER['PHP_SELF'] . '?date=';
if ($month == 12) {
$next_link .= mktime(0, 0, 0, 1, $day, ($year + 1));
} else {
$next_link .= mktime(0, 0, 0, ($month + 1), $day, $year);
}
$next_link .= '">Next >></a>';
// format month dates
$month_start = date('Y-m-d', $month_start);
$month_end = date('Y-m-d', $month_end);
// query for events
$query = "
SELECT *
FROM date
WHERE startdate >= '$month_start'
AND enddate <= '$month_end'";
echo $query;
$result = mysql_query($query);
if(!$result) error_message(sql_error());
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
//format sql dates in php date
$startdatevalues = explode("-", $row["startdate"]);
$startdate = mktime(0, 0, 0, $startdatevalues[1], $startdatevalues[2], $startdatevalues[0]);
$enddatevalues = explode("-", $row["enddate"]);
$enddate = mktime(0, 0, 0, $enddatevalues[1], $enddatevalues[2], $enddatevalues[0]);
//add to array
$events['event'.$i]['title'] = $row['eventtitle'];
$events['event'.$i]['startdate'] = $startdate;
$events['event'.$i]['enddate'] = $enddate;
$i++;
}
echo '<table border="1" cellpadding="2" cellspacing="0" width="300" class="calendar">' . "\n" .
'<tr>' . "\n" .
'<td colspan="7">' . "\n" .
'<table align="center">' . "\n" .
'<tr>' . "\n" .
'<td colspan="2" width="75" align="left">' . $previous_link . '</td>' . "\n" .
'<td colspan="3" width="150" align="center">' . $month_name . $year . '</td>' . "\n" .
'<td colspan="2" width="75" align="right">' . $next_link . '</td>' . "\n" .
'</tr>' . "\n" .
'</table>' . "\n" .
'</td>' . "\n" .
'<tr>' . "\n" .
'<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>' . "\n" .
'</tr>' . "\n" .
$i = 0;
foreach($weeks AS $week) {
echo "<tr>\n";
foreach ($week as $d) {
if ($i < $offset_count) {
$day_link = '<a href="' . $_SERVER['PHP_SELF'] . '?date=' . mktime(0, 0, 0, $month - 1, $d, $year) . '">' . $d . '</a>';
echo '<td class="nonmonthdays">' . $day_link . '</td>' . "\n";
}
if (($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)) {
$i = 1;
while (isset($events['event'.$i]))
{
if (
(date('d',$events['event'.$i]['startdate']) < $d AND date('d',$events['event'.$i]['enddate']) > $d )
OR
(date('d',$events['event'.$i]['startdate']) == $d
OR
date('d',$events['event'.$i]['enddate']) == $d
))
{
$today_class = 'eventtoday';
$days_class = 'eventmonthdays';
}else{
$today_class = 'today';
$days_class = 'monthdays';
}
$i++;
}
$day_link = '<a href="' . $_SERVER['PHP_SELF'] . '?date=' . mktime(0, 0, 0, $month, $d, $year) . '">' . $d . '</a>';
if ($date == mktime(0, 0, 0, $month, $d, $year)) {
echo '<td class="' . $today_class . '">' . $d . '</td>'. "\n";
} else {
echo '<td class="' . $days_class . '">' . $day_link . '</td>' . "\n";
}
} elseif (($outset > 0)) {
if (($i >= ($num_weeks * 7) - $outset)) {
$day_link = '<a href="' . $_SERVER['PHP_SELF'] . '?date=' . mktime(0, 0, 0, $month + 1, $d, $year) . '">' . $d . '</a>';
echo '<td class="nonmonthdays">' . $day_link . '</td>' . "\n";
}
}
$i++;
}
echo '</tr>' . "\n";
}
echo '<tr><td colspan="7" class="days"> </td></tr>';
echo '</table>';
?>
</body>
</html>