Jump to content

Modifying a calendar


Matpatnik

Recommended Posts

Hi guys,I'm using a code from http://web-developer.sitecritic.net/quick_calendar.txt but I have some problem with it. It doesn't show the link from db and I'm not able to navigate through the months and years. Here a link to my page http://www.runefr.com/lpo/index.php?sectio...dar〈=en, can someone help me with it, I'm kind of lost with ajax and oop.

<?php// This year$y = date('Y');// This month$m = date('n');// This Day$d = date('j');$today = array('day'=>$d, 'month'=>$m, 'year'=>$y);// If user specify Day, Month and Year, reset the varif (isset($_GET['m'])) {	$y = $_GET['y'];	$m = $_GET['m'];}if ($_GET['lang'] == "en") {	$lang = "calendar_desc_en";	$semaine = "<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>Th</th><th>F</th><th>S</th></tr>";} else {	$lang = "calendar_desc_fr";	$semaine = "<tr><th>D</th><th>L</th><th>M</th><th>M</th><th>J</th><th>V</th><th>S</th></tr>";}include "include/conn.inc.php";// name of css$css = "calendar";// Location of the calendar script file from the root$ajaxPath = "http://www.runefr.com/lpo/page/quick_calendar.php?section=calendar&lang=" . $_GET['lang'] . "&m=";// END OF CONFIGURATION.$sql = "SELECT * 		FROM calendar 		WHERE (calendar_month='" . $m . "' AND calendar_year='" . $y . "') || 			  (calendar_month='*' AND calendar_year='" . $y . "') || 			  (calendar_month='" . $m . "' AND calendar_year='*') || 			  (calendar_month='*' AND calendar_year='*')";$rs = mysql_query($sql);$links = array(); while ($rw = mysql_fetch_array($rs)) {	extract($rw);	$links[] = array('calendar_day'=>$day, 'calendar_month'=>$month, 'calendar_year'=>$year, 'calendar_link'=>$link, $lang=>$desc);}?><?php // if called via ajax, dont display javascript againif (!isset($_GET['ran'])) {	$extra .=<<<CONTENT<script language="javascript">function createQCObject() {    var req;    if(window.XMLHttpRequest){ 	  // Firefox, Safari, Opera... 	  req = new XMLHttpRequest();    } else if(window.ActiveXObject) { 	  // Internet Explorer 5+ 	  req = new ActiveXObject("Microsoft.XMLHTTP");    } else { 	  alert('Problem creating the XMLHttpRequest object');    }    return req; } // Make the XMLHttpRequest object var http = createQCObject(); function displayQCalendar(m,y) {	var ran_no=(Math.round((Math.random()*9999))); 	http.open('get', '$ajaxPath'+m+'&y='+y+'&ran='+ran_no);   	http.onreadystatechange = function() {		if(http.readyState == 4 && http.status == 200) { 			  var response = http.responseText;			  if(response) { 				document.getElementById("quickCalender").innerHTML = http.responseText; 			  }    		} 	}    	http.send(null); }</script>CONTENT;}?><?phpclass CreateQCalendarArray {	var $daysInMonth;	var $weeksInMonth;	var $firstDay;	var $week;	var $month;	var $year;	function CreateQCalendarArray($month, $year) {		$this->month = $month;		$this->year = $year;		$this->week = array();		$this->daysInMonth = date("t",mktime(0,0,0,$month,1,$year));		// get first day of the month		$this->firstDay = date("w", mktime(0,0,0,$month,1,$year));		$tempDays = $this->firstDay + $this->daysInMonth;		$this->weeksInMonth = ceil($tempDays/7);		$this->fillArray();	}		function fillArray() {		// create a 2-d array		for($j=0;$j<$this->weeksInMonth;$j++) {			for($i=0;$i<7;$i++) {				$counter++;				$this->week[$j][$i] = $counter; 				// offset the days				$this->week[$j][$i] -= $this->firstDay;				if (($this->week[$j][$i] < 1) || ($this->week[$j][$i] > $this->daysInMonth)) {						$this->week[$j][$i] = "";				}			}		}	}}class QCalendar {		var $html;	var $weeksInMonth;	var $week;	var $month;	var $year;	var $today;	var $links;	var $css;	function QCalendar($cArray, $today, $links, $css='') {		$this->month = $cArray->month;		$this->year = $cArray->year;		$this->weeksInMonth = $cArray->weeksInMonth;		$this->week = $cArray->week;		$this->today = $today;		$this->links = $links;		$this->css = $css;		$this->createHeader();		$this->createBody();		$this->createFooter();	}		function createHeader() {		  $header = date('M', mktime(0,0,0,$this->month,1,$this->year)).' '.$this->year;		  $nextMonth = $this->month+1;		  $prevMonth = $this->month-1;		  // thanks adam taylor for modifying this part		switch($this->month) {			case 1:	   			$lYear = $this->year;   				$pYear = $this->year-1;   				$nextMonth=2;   				$prevMonth=12;   			break;			  case 12:   				$lYear = $this->year+1;   				$pYear = $this->year;   				$nextMonth=1;   				$prevMonth=11;			  break;			  default:				  $lYear = $this->year;	   			$pYear = $this->year;			  break;		  }		// --		$this->html = "<table cellspacing='0' cellpadding='0' class='$this->css'>		<tr>		<th class='header'> <a href=\"java script:;\" onclick=\"displayQCalendar('$this->month','".($this->year-1)."')\" class='headerNav' title='Prev Year'><<</a></th>		<th class='header'> <a href=\"java script:;\" onclick=\"displayQCalendar('$prevMonth','$pYear')\" class='headerNav' title='Prev Month'><</a></th>		<th colspan='3' class='header'>$header</th>		<th class='header'><a href=\"java script:;\" onclick=\"displayQCalendar('$nextMonth','$lYear')\" class='headerNav' title='Next Month'>></a> </th>		<th class='header'> <a href=\"java script:;\" onclick=\"displayQCalendar('$this->month','".($this->year+1)."')\"  class='headerNav' title='Next Year'>>></a></th>		</tr>";	}		function createBody(){		if ($_GET['lang'] == "en") {			$semaine = "<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>Th</th><th>F</th><th>S</th></tr>";		} else {			$semaine = "<tr><th>D</th><th>L</th><th>M</th><th>M</th><th>J</th><th>V</th><th>S</th></tr>";		}		// start rendering table		$this->html.= $semaine;		for($j=0;$j<$this->weeksInMonth;$j++) {			$this->html.= "<tr>";			for ($i=0;$i<7;$i++) {				$cellValue = $this->week[$j][$i];				// if today				if (($this->today['day'] == $cellValue) && ($this->today['month'] == $this->month) && ($this->today['year'] == $this->year)) {					$cell = "<div class='today'>$cellValue</div>";				}				// else normal day				else {					$cell = "$cellValue";				}				// if days with link				foreach ($this->links as $val) {					if (($val['calendar_day'] == $cellValue) && (($val['calendar_month'] == $this->month) || ($val['calendar_month'] == '*')) && (($val['calendar_year'] == $this->year) || ($val['calendar_year'] == '*'))) {						$cell = "<div class=\"link\"><a href=\"{$val['calendar_link']}\" title='{$val['calendar_desc']}'>$cellValue</a></div>";						break;					}				}					$this->html.= "<td>$cell</td>";			}			$this->html.= "</tr>";		}		}		function createFooter() {		if ($_GET['lang'] == "en") {			$aujourdhui = "Today is";		} else {			$aujourdhui = "Nous sommes le";		}		$this->html .= "<tr><td colspan=\"7\" class=\"footer\"><a href=\"java script:;\" onclick=\"displayQCalendar('{$this->today['month']}','{$this->today['year']}')\" class='footerNav'>$aujourdhui {$this->today['day']} ".date('M', mktime(0,0,0,$this->today['month'],1,$this->today['year']))." {$this->today['year']}</a></td></tr></table>";	}		function render() {		global $content;		$content .= $this->html;	}}// render calendar now$cArray = &new CreateQCalendarArray($m, $y);$cal = &new QCalendar($cArray, $today, $links, $css);if (!isset($_GET['ran'])) {	$content .= "<div id=\"quickCalender\">";}$cal->render();if (!isset($_GET['ran'])) {	$content .= "</div>";}?>

Thank you for your help

Link to comment
Share on other sites

Have you done any debugging? You should insert debugging statements into the code to do things like print out the results from the database (see if there are any), insert alerts in the Javascript functions to let you know what the code is about to do, etc. That way you find out what's running and what's not. That's the first step.Also, in the lines like these to print out the links: <th class='header'> <a href=\"java script:;\" onclick=\"displayQCalendar('$this->month','".($this->year-1)."')\" class='headerNav' title='Prev Year'><<</a></th>The extra < or > characters should be replaced with their HTML entities.

Link to comment
Share on other sites

exactly what he said, print out the results of various operations, for isntance, add 'echo $y;' after your third line to make sure that function is working properly (trying to see if standard php functions are working properly is fairly pointless, so thats a mildly bad example, but hopefully you get the idea)

Link to comment
Share on other sites

Add alerts into the Javascript functions, so that when you click the link and nothing happens, you get popups telling you that the function is getting executed, the value of some variable, whatever. When you do stuff like that, you can narrow it down to the specific line that is causing the code to fail, and then you just look at that line to determine why.When people debug code, they don't just read through the code and try to see stuff that looks wrong. They insert statements into the code so that when they run the code, the code outputs a bunch of extra information that tells the programmer what the code is doing so that they know when and why the code stops.

Link to comment
Share on other sites

is $var=>$var2 is the same as $var2=$var ? I know that $var=>$var2 is in an array.can I do something like this?instead of this

$sql = "SELECT *		FROM calendar		WHERE (calendar_month='" . $m . "' AND calendar_year='" . $y . "') ||			  (calendar_month='*' AND calendar_year='" . $y . "') ||			  (calendar_month='" . $m . "' AND calendar_year='*') ||			  (calendar_month='*' AND calendar_year='*')";$rs = mysql_query($sql);$links = array();while ($rw = mysql_fetch_array($rs)) {	extract($rw);	$links[] = array('calendar_day'=>$day, 'calendar_month'=>$month, 'calendar_year'=>$year, 'calendar_link'=>$link, $lang=>$desc);}

doing this

$sql = "SELECT *		FROM calendar		WHERE (calendar_month='" . $m . "' AND calendar_year='" . $y . "') ||			  (calendar_month='*' AND calendar_year='" . $y . "') ||			  (calendar_month='" . $m . "' AND calendar_year='*') ||			  (calendar_month='*' AND calendar_year='*')";$rs = mysql_query($sql);$links = array();while ($rw = mysql_fetch_array($rs)) {	$day = $rw['calendar_day'];	$month = ['calendar_month'];	$year = ['calendar_year'];	$link = ['calendar_link'];	$links[] = array($day, $month, $year, $link, $lang=>$desc);}

will it do the same thing?

Link to comment
Share on other sites

No, in fact the last one isn't even syntactically valid code. This syntax:array('calendar_day'=>$day, 'calendar_month'=>$month,Is for creating an array. That is pretty much the same as doing this:$array['calendar_day'] = $day;$array['calendar_month'] = $month;If you want to duplicate this entire line:$links[] = array('calendar_day'=>$day, 'calendar_month'=>$month, 'calendar_year'=>$year, 'calendar_link'=>$link, $lang=>$desc);You would have to do this:$tmp = array();$tmp['calendar_day'] = $day;$tmp['calendar_month'] = $month;$tmp['calendar_year'] = $year;$tmp['calendar_link'] = $link;$tmp[$lang] = $desc;$links[] = $tmp;So, it takes several lines to duplicate what that one line does.This code is not valid:

	$day = $rw['calendar_day'];	$month = ['calendar_month'];	$year = ['calendar_year'];	$link = ['calendar_link'];

Each line needs the array reference, you only have it on the first line. This would be correct:

	$day = $rw['calendar_day'];	$month = $rw['calendar_month'];	$year = $rw['calendar_year'];	$link = $rw['calendar_link'];

But this line still doesn't do the same thing:$links[] = array($day, $month, $year, $link, $lang=>$desc);That creates an array where $links[][0] = $day, $links[][1] = $month, etc, the elements are numbered. The original version had the elements named, so that $links[]['calendar_day'] = $day instead of $links[][0].

Link to comment
Share on other sites

I see, so if I erase the line $links = array(); it will give me an array like that: $links['calendar_day'] = $dayI think this is where the problem is for the db.for this part I forgot to write the $rw for the array sorry about that

	$day = $rw['calendar_day'];	$month = ['calendar_month'];	$year = ['calendar_year'];	$link = ['calendar_link'];

Link to comment
Share on other sites

I see, so if I erase the line $links = array(); it will give me an array like that: $links['calendar_day'] = $day
I'm not sure where you got that from, that's not true. You need the line to initialize $links regardless of what type of array it is. Whether the array has numerical keys or strings as keys is dependent on how you build the array, not on how you declare it. I would stick with the original code:$links[] = array('calendar_day'=>$day, 'calendar_month'=>$month, 'calendar_year'=>$year, 'calendar_link'=>$link, $lang=>$desc);It keeps it short and sweet. After you build the array, print it out to see what it did:
while ($rw = mysql_fetch_array($rs)) {	...}print_r($links);

Link to comment
Share on other sites

this is what I got from the print_r: Array ( [calendar_day] => [calendar_month] => [calendar_year] => [calendar_link] => [calendar_desc_fr] => )

Link to comment
Share on other sites

OK. If you look at that using View Source instead of in the browser, you'll see it formatted:

Array (   [calendar_day] =>   [calendar_month] =>   [calendar_year] =>   [calendar_link] =>   [calendar_desc_fr] => )

That is just saying that the array has 5 elements, and you see what the names are, but none of them have a value. If they had values, you would see something like this:

Array (   [calendar_day] => 3  [calendar_month] => "June"   [calendar_year] => 2007  [calendar_link] =>   [calendar_desc_fr] => )

etc. So the array is not getting values saved for whatever reason. What code are you using now?

Link to comment
Share on other sites

I still use the same code as my first postthis is the begainning of it:

<?php// This year$y = date('Y');// This month$m = date('n');// This Day$d = date('j');$today = array('day'=>$d, 'month'=>$m, 'year'=>$y);// If user specify Day, Month and Year, reset the varif (isset($_GET['m'])) {	$y = $_GET['y'];	$m = $_GET['m'];}if ($_GET['lang'] == "en") {	$lang = "calendar_desc_en";	$semaine = "<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>Th</th><th>F</th><th>S</th></tr>";} else {	$lang = "calendar_desc_fr";	$semaine = "<tr><th>D</th><th>L</th><th>M</th><th>M</th><th>J</th><th>V</th><th>S</th></tr>";}include "include/conn.inc.php";// name of css$css = "calendar";// Location of the calendar script file from the root$ajaxPath = "http://www.runefr.com/lpo/page/quick_calendar.php?section=calendar&lang=" . $_GET['lang'] . "&m=";// END OF CONFIGURATION.$sql = "SELECT * 		FROM calendar 		WHERE (calendar_month='" . $m . "' AND calendar_year='" . $y . "') || 			  (calendar_month='*' AND calendar_year='" . $y . "') || 			  (calendar_month='" . $m . "' AND calendar_year='*') || 			  (calendar_month='*' AND calendar_year='*')";$rs = mysql_query($sql);$links = array(); while ($rw = mysql_fetch_array($rs)) {	extract($rw);	$links[] = array('calendar_day'=>$day, 'calendar_month'=>$month, 'calendar_year'=>$year, 'calendar_link'=>$link, $lang=>$desc);}?>

I think I know what was my mistake, by extracting $rw the column name become $column so the $link should be like this:

$links[] = array('day'=>$calendar_day, 'month'=>$calendar_month, 'year'=>$calendar_year, 'link'=>$calendar_link, $lang=>$desc);

well I think

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...