Location: PHPKode > scripts > PHP calendar with AJAX driven agenda > MyWeb20Agenda/MyWeb20Agenda/includes/calendar.php
<?php
	// connect to database
	require("calendar_conn.php");
	
	$dblogin = 'calendar_conn.php';
	$deleteid = $_REQUEST['deleteid'];
	$insertid = $_REQUEST['id'];
	$m = $_REQUEST['m'];
	$y = $_REQUEST['y'];
	
	// check if an insertion process is present
	if($insertid == '1')
	{		
		$task = $_REQUEST['task'];
		$lokation = $_REQUEST['lokation'];
		$importance = $_REQUEST['importance'];
		$status = $_REQUEST['status'];
		$year = $_REQUEST['year'];
		$month = $_REQUEST['month'];
		$day = $_REQUEST['days'];
		$start = $_REQUEST['start'];
		$end = $_REQUEST['end'];
		$todo = $_REQUEST['todo'];
		$dates = $year."-".$month."-".$day;
		
		$fields = 	"date_id,task,lokation,importance,status,dates,starttime,endtime,todo";
		$tbvalues = "NULL,'$task','$lokation','$importance','$status','$dates','$start','$end','$todo'";
		$tabname = "agenda";
		$goodnews = "";
		$badnews = "Data has not been inserted.";	
		require_once 	'../class/agenda.data.class.php';
		$insagenda = new SelectData($dblogin,$fields,$tabname,$data,$selective,$standard,
								$selectid,$selectorder,$goodnews,$badnews,$noquery,
								$message,$deleteid,$insertid,$tbvalues,$days,$updateid,
								$updfieldid,$m,$y);
		echo $insagenda->FillTable();
	}
	
	// check if a deletion process is present
	if($deleteid > '0')
	{
		$fields = 	"date_id";
		$deleteid = $deleteid;
		$tabname = "agenda";
		$goodnews = "";
		$badnews = "Data has not been deleted.";	
		require_once 	'../class/agenda.data.class.php';
		$delagenda = new SelectData($dblogin,$fields,$tabname,$data,$selective,$standard,
								$selectid,$selectorder,$goodnews,$badnews,$noquery,
								$message,$deleteid,$insertid,$tbvalues,$days,$updateid,
								$updfieldid,$m,$y);
		echo $delagenda->DeleteRow();
	}
	
	// set and get variable to create data for the calendar
	if($m == '' && $y == '')
	{			
		$day = date("d");				
		$month = date("m");
		$year = date("Y");	
		$days = date('t',mktime(1,1,1,$month,$day,$year))+1;		
		$day_one = date('w',mktime(1,1,1,$month,1,$year))-1;  
		$caltitle = date("F Y");
		$m = $month;
		$y = $year;
	} else {
		$tag = date("d");	
		$days = date("t", mktime(0, 0, 0, $m , 2 , $y))+1;
		$mon = date("F", mktime(0, 0, 0, $m , 2 , $y));
		$year  = date("Y", mktime(0, 0, 0, $m , 2 , $y));
		$day_one = date('w',mktime(0,0,0,$m,0,$y));			
		$caltitle = $mon."\n".$year;
		if($m == date("m")) {$day = $tag;}
	}
	
	// calculate start variables for navigation of the calendar
	if(!isset($m) && (!isset($y)))
	{
		if($month == '12')
		{
			$pm = $month-1;
			$py = $year;
			$nm = $month-11;
			$ny = $year+1;
		}
		elseif($month == '1')
		{
			$pm = $month+11;
			$py = $year-1;
			$nm = $month+1;
			$ny = $year;
		}
		else
		{
			$pm = $month-1;
			$py = $year;
			$nm = $month+1;
			$ny = $year;
		}
	}
	
	// calculate proceeding variables for navigation of the calendar
	if(isset($m) && isset($y))
	{
		if($m == '12')
		{
			$nm = $m-11;
			$ny = $y+1;
			$pm = $m-1;
			$py = $y;
		} elseif($m < '1') {
			$nm = $m+1;
			$ny = $y;
			$pm = $m+11;
			$py = $y-1;
		} else {
			$nm = $m+1;
			$ny = $y;
			$pm = $m-1;
			$py = $y;
		}
	}
	
	// set the correct variables to match startday with loop start
	if($day_one == '0') { $span = $day_one+1;} else { $span = $day_one;}
	if($day_one == '1') { $span = $day_one+1; }
	if($day_one == '-1') { $span = $day_one+7;}
	$weekdays = '7';	
	$liste = $days;
	$firstday = $weekdays-$span+1;

?>
<!-- Start calendar with agenda  -->
<?php
echo("
		<table width='210' height='190' border='0' cellspacing='2' cellpadding='0'>
			<tr>
				<td class=\"tabtitle\" colspan='7'><a href='#' onClick=\"prevnextCalendar($pm,$py);\"><</a>&nbsp;$caltitle&nbsp;<a href='#' onClick=\"prevnextCalendar($nm,$ny);\">></a></td>
			</tr>
			<tr>
				<td class=\"tabdays\">Mon</td>
				<td class=\"tabdays\">Tue</td>
				<td class=\"tabdays\">Wed</td>
				<td class=\"tabdays\">Thu</td>
				<td class=\"tabdays\">Fri</td>
				<td class=\"tabdays\">Sat</td>
				<td class=\"tabdays\">Sun</td>
			</tr>");
?>
<?php
	// query table and get the variables to compare agenda entries with calendar day
	$sql = 	"SELECT * FROM agenda 
			WHERE MONTH(dates) = '$m' 
			AND YEAR(dates) = '$y' ";

	$result = mysql_query ($sql, $link);
	$termday = array();
	while (list ($date_id,$task,$lokation,$importance,$status,$dates,$starttime,$endtime,$todo) = mysql_fetch_row ($result)) 
	{
		$termday[$date_id] = substr($dates, 8, 10);
	}
?>
<?php
	// start to create rows and cols for the agenda
	// set variables for the first row if the first day is not a monday	
	echo "<tr>\r";
	if($span > '0')
	{
		if($span != '1' && $span != '2')
		{
			$setCol = "<td colspan=\"$span\"></td>\r";
			echo $setCol;
		} 
		elseif($span == '1') 
		{
			$firstday = $firstday+1;
		}
		elseif($span == '2')
		{
			$firstday = $firstday+1;
			$setCol = "<td colspan=\"1\"></td>\r";
			echo $setCol;
		}
		for ($i = 1; $i < $firstday; $i++) 
		{
			$dayvar = array($i);
			if (in_array($i, $termday))
			{
				$setCol = "<td class=\"matchday\"><a href='#' onClick='showAgenda($y,$m,$i);'>$i</a></td>\r";
			}
			elseif (in_array($day, $dayvar))
			{
				$setCol = "<td class=\"now\">$i</td>\r";
			}
			else
			{
				$setCol = "<td class=\"tdc\">$i</td>\r";
			}
			echo $setCol;	
		}
	}

	echo "</tr>\r";
	echo "<tr>\r";

	// set variables for the rest of the rows if the first day is a monday
	for ($i = $firstday; $i < $liste; $i++) 
	{		
		$dayvar = array($i);
		if($i % 7 != '0' && $span == '1' )
		{
			if (in_array($i, $termday))
			{
				$setCol = "<td class=\"matchday\"><a href='#' onClick='showAgenda($y,$m,$i);'>$i</a></td>\r";
				echo $setCol;
			}
			elseif (in_array($day, $dayvar))
			{
				$setCol = "<td class=\"now\">$i</td>\r";
				echo $setCol;
			}
			else
			{
				$setCol = "<td class=\"tdc\">$i</td>\r";
				echo $setCol;
			}
		} 
		
		// set variables for the rest of the rows if the first day is not a monday
		elseif($i % $weekdays-$firstday+1 != '0' && $span != '1')
		{
			if (in_array($i, $termday))
			{
				$setCol = "<td class=\"matchday\"><a href='#' onClick='showAgenda($y,$m,$i);'>$i</a></td>\r";
			}
			elseif (in_array($day, $dayvar))
			{
					$setCol = "<td class=\"now\">$i</td>\r";
			}
			else
			{
				$setCol = "<td class=\"tdc\">$i</td>\r";
			}
			echo $setCol;
		} 
		else 
		{ 	
			if (in_array($i, $termday))
			{
				$setCol = "<td class=\"matchday\"><a href='#' onClick='showAgenda($y,$m,$i);'>$i</a></td></tr><tr>\r";
			}
			elseif (in_array($day, $dayvar))
			{
					$setCol = "<td class=\"now\">$i</td><tr>\r";
			}
			else
			{
				$setCol = "<td class=\"tdc\">$i</td></tr><tr>\r";
			}
			echo $setCol;
		}
	}

?>
<?php
echo ("<tr><td colspan=\"7\" height=\"13\" class=\"tabdays\"><a href=\"#\" onClick=\"showAgendaForm($days,$y,$m);\">New Entry</a></td></tr>
</table>");
?>
<!-- End calendar with agenda  -->
Return current item: PHP calendar with AJAX driven agenda