Howdy,
I have the following problem with a PHP calendar.
Source is from this thread
In an array I have the events stored wich come from a database, now I want to display them in the calendar (with a different color). The Array contains ca_title, ca_startdate & ca_enddate.
The calendar works fine without events however when trying to display events I get troubles 🙂
The table Calendar looks like
ca_id
ca_userid
ca_title
ca_desc
ca_startdate date NOT NULL default '0000-00-00'
ca_enddate date NOT NULL default '0000-00-00'
ca_date
ca_type
The calendar displays on index.php and includes calendar.php
Calendar.php code
<?php
// Gather variables from
// user input and break them
// down for usage in our script
if(!isset($_REQUEST['date'])){
$date = mktime(0,0,0,date('m'), date('d'), date('Y'));
} else {
$date = $_REQUEST['date'];
}
$currentdate = mktime(0,0,0,date('m'), date('d'), date('Y'));
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$currentday = date('d', $currentdate);
$currentmonth = date('m', $currentdate);
$currentyear = date('Y', $currentdate);
//$startdate = mktime(0,0,0,06, 03, 2006);
$startdate = $unixtimestamp;
//echo "startdate = " . $startdate . "<Br>currentdate =" . $date;
// Get the first day of the month
$month_start = mktime(0,0,0,$month, 1, $year);
// Get friendly month name
$month_name = date('M', $month_start);
// Figure out which day of the week
// the month starts on.
$month_start_day = date('D', $month_start);
switch($month_start_day){
case "Sun": $offset = 0; break;
case "Mon": $offset = 1; break;
case "Tue": $offset = 2; break;
case "Wed": $offset = 3; break;
case "Thu": $offset = 4; break;
case "Fri": $offset = 5; break;
case "Sat": $offset = 6; break;
}
// determine how many days are in the last month.
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);
}
// determine how many days are in the current month.
$num_days_current = cal_days_in_month(0, $month, $year);
// Build an array for the current days
// in the month
for($i = 1; $i <= $num_days_current; $i++){
$num_days_array[] = $i;
}
// Build an array for the number of days
// in last month
for($i = 1; $i <= $num_days_last; $i++){
$num_days_last_array[] = $i;
}
// If the $offset from the starting day of the
// week happens to be Sunday, $offset would be 0,
// so don't need an offset correction.
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);
}
// The else statement is to prevent building the $offset array.
else {
$offset_count = 0;
$new_count = $num_days_array;
}
// count how many days we have with the two
// previous arrays merged together
$current_num = count($new_count);
// Since we will have 5 HTML table rows (TR)
// with 7 table data entries (TD)
// we need to fill in 35 TDs
// so, we will have to figure out
// how many days to appened to the end
// of the final array to make it 35 days.
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;
}
// Outset Correction
for($i = 1; $i <= $outset; $i++){
$new_count[] = $i;
}
// Now let's "chunk" the $all_days array
// into weeks. Each week has 7 days
// so we will array_chunk it into 7 days.
$weeks = array_chunk($new_count, 7);
?>
And index.php contains the following code for the calendar
<?
$db_connection = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db);
//CALENDAR
//$q_calendar = "SELECT * FROM calendar WHERE ca_startdate>='$year-$month-1' ORDER BY ca_startdate ASC";
$q_calendar = "SELECT * FROM calendar WHERE ca_startdate>='$year-$month-1' AND ca_startdate<='$year-$month-31' ORDER BY ca_startdate ASC";
$r_calendar = mysql_query($q_calendar) or die(mysql_error());
$num_calendar = mysql_num_rows($r_calendar);
//CALENDAR
mysql_close ($db_connection);
?>
// ***********************
//HTML CODE FOR PAGE LAYOUT
// ***********************
<!-- CALENDAR -->
<?
//SHOW THE DATES WHEN AN EVENT IS
while ($obj_calendar = mysql_fetch_object($r_calendar)) {
$arr_cal[] = array('title'=>$obj_calendar->ca_title, 'startdate'=>$obj_calendar->ca_startdate, 'enddate'=>$obj_calendar->ca_enddate);
}
//SHOW THE DATES WHEN AN EVENT IS
//print_r($arr_cal);
//echo "<br><br>";
//BUILD LINKS
$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>";
//BUILD LINKS
$cal_header1 = "$previous_link <b>$month_name $year</b> $next_link";
echo "<table width=\"190\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"15\" align=\"center\" valign=\"middle\" background=\"images/skins/green/cal_top.jpg\" class=\"font_white_size10\">$cal_header1</td></tr><tr><td height=\"5\" align=\"center\" valign=\"middle\" background=\"images/skins/green/cal_middle.jpg\"><!-- EMPTY --></td> </tr><tr><td align=\"center\" valign=\"middle\" background=\"images/skins/green/cal_middle1.jpg\"><table width=\"190\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"27\" height=\"20\" align=\"center\" valign=\"middle\" class=\"font_white_size10\">Su</td><td width=\"27\" align=\"center\" valign=\"middle\" class=\"font_white_size10\">Mo</td><td width=\"27\" align=\"center\" valign=\"middle\" class=\"font_white_size10\">Te</td><td width=\"27\" align=\"center\" valign=\"middle\" class=\"font_white_size10\">We</td><td width=\"27\" align=\"center\" valign=\"middle\" class=\"font_white_size10\">Th</td><td width=\"27\" align=\"center\" valign=\"middle\" class=\"font_white_size10\">Fr</td><td width=\"27\" align=\"center\" valign=\"middle\" class=\"font_white_size10\">Sa</td></tr></table></td></tr><tr><td height=\"5\" align=\"center\" valign=\"middle\" background=\"images/skins/green/cal_middle.jpg\"><!-- EMPTY --></td></tr>";
echo "<tr><td align=\"center\" valign=\"middle\" background=\"images/skins/green/cal_middle.jpg\" class=\"font_black_size10\"><table width=\"190\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
$i = 0;
foreach($weeks AS $week){
echo "<tr>\n";
foreach($week as $d){
//COLOR THE CURRENT DAY THIS MONTH GREEN -> #7C8C4B
if(($currentday == $d) && ($currentmonth == $month)){
$cal_bgcolor = "bgcolor=\"#7C8C4B\"";
}
else {
$cal_bgcolor = "";
}
//COLOR THE CURRENT DAY THIS MONTH GREEN -> #7C8C4B
//PREVIOUS WEEK
if($i < $offset_count){
$day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month -1,$d,$year)."\">$d</a>";
$cal_day = " ";
}
//PREVIOUS WEEK
//THIS WEEK
if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){
$day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month,$d,$year)."\" $cal_tooltip>$d</a>";
if($date == mktime(0,0,0,$month,$d,$year)){
$cal_day = "<b>$d</b>";
} else {
$cal_day = "$day_link";
}
}
//THIS WEEK
//NEXT WEEK
elseif(($outset > 0)) {
if(($i >= ($num_weeks * 7) - $outset)){
$cal_day = " ";
}
}
//NEXT WEEK
//PRINT THE TABLE
echo "<td widht=\"27\" height=\"17\" align=\"center\" valign=\"middle\" $cal_bgcolor class=\"font_black_size10\">\n";
echo $cal_day;
echo "</td>\n";
//PRINT THE TABLE
$i++;
}
echo "</tr>\n";
}
echo "</table></td></tr>";
echo "<tr><td height=\"5\" align=\"center\" valign=\"middle\" background=\"images/skins/green/cal_bottom.jpg\"><!-- EMPTY --></td></tr></table>";
?>
<!-- CALENDAR -->
// ***********************
//HTML CODE FOR PAGE LAYOUT
// ***********************
The print_r($arr_cal); contains
Array ( [0] => Array ( [title] => Test [startdate] => 2006-06-06 [enddate] => 2006-06-08 ) [1] => Array ( [title] => Vacation [startdate] => 2006-06-24 [enddate] => 2006-07-01 ) )
Now I can display only 1 array (array 0) only when using this query
$q_calendar = "SELECT * FROM calendar WHERE ca_startdate>='$year-$month-1';
With this query I get no results
$q_calendar = "SELECT * FROM calendar WHERE ca_startdate>='$year-$month-1' AND ca_startdate<='$year-$month-31' ORDER BY ca_startdate ASC";
However both times print_r($arr_cal); shows values, 1 time sorted and 1 time not sorted.
Any idea's what I am doing wrong ?
Me is 😕