Jump to content

Help with event calendar


glovebox

Recommended Posts

I'm trying to get an event calendar up and running using a tutorial I found here: http://www.evolt.org/quick_calendar_using_ajax_and_php. I've got everything customized the way I need it to, but I'm running into problems listing all events for a day. I can get it to list the first event for a day pretty easily using this code:

 

<?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 var
if (isset($_GET['m'])) {
$y = $_GET['y'];
$m = $_GET['m'];
}

// CONFIGURE THE DB ACCESS
$dbhost = '########';
$dbuser = '########';
$dbpass = '########';
$database = "########";
$dbConnect = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$dbConnect) {
   die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($database, $dbConnect);
if (!$db_selected) {
   die ('db selection error : ' . mysql_error());
}

// name of table
$tableName = 'calendar';

// name of css
$css = 'calendar';

// Location of the calendar script file from the root
$ajaxPath = 'calendar.php';

// END OF CONFIGURATION. YOU CAN CHANGE THE CSS. THE OTHER CODES CAN BE KEPT AS DEFAULT IF YOU WANT.

$sql = "SELECT * FROM $tableName WHERE (month='$m' AND year='$y') || (month='*' AND year='$y') || (month='$m' AND year='*') || (month='*' AND year='*')";

$rs = mysql_query($sql);
$links = array(); 
while ($rw = mysql_fetch_array($rs)) {
extract($rw);
$links[] = array('day'=>$day, 'month'=>$month, 'year'=>$year, 'link'=>$link, 'desc'=>$desc);
}

?>
<?php 
// if called via ajax, dont display style sheet and javascript again
if (!isset($_GET['ran'])) {
?>
<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='+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>
<?php 
}
?>
<?php
class 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
	// to try and start w/ monday, replace 1 below with 0;
	$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 = "<div id='tableheader'><a href=\"javascript:;\" onclick=\"displayQCalendar('$prevMonth','$pYear')\" title='Prev Month'>«</a> $header <a href=\"javascript:;\" onclick=\"displayQCalendar('$nextMonth','$lYear')\" title='Next Month'>»</a> 
	</div><table cellspacing='0' cellpadding='0' class='$this->css'>
	";
}

function createBody(){
	// start rendering table
	$this->html.= "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
	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><span class=\"today_cal\">$cellValue</span></div>";
			}
			// else normal day
			else {
				$cell = "$cellValue  ";
			}
			// if days with link
			foreach ($this->links as $val) {
				if (($val['day'] == $cellValue) && (($val['month'] == $this->month) || ($val['month'] == '*')) && (($val['year'] == $this->year) || ($val['year'] == '*'))) {
	if (($this->today['day'] == $cellValue) && ($this->today['month'] == $this->month) && ($this->today['year'] == $this->year)) {
				$date_class = "today_cal";
				}
				else {
				$date_class = "link";
				}
				$cell = "<div><span class='$date_class'>$cellValue</span><br /><a href=\"{$val['link']}\" title='{$val['desc']}' class='calLink'>{$val['desc']}</a></div>";
					break;
				}
			}	
			$this->html.= "<td>$cell</td>";
		}
		$this->html.= "</tr>";
	}	
}

function createFooter() {
	$this->html .= "</table><div id='tablefooter'><a href=\"javascript:;\" onclick=\"displayQCalendar('{$this->today['month']}','{$this->today['year']}')\">Today »</a>";
}

function render() {
	echo $this->html;
}
}
?>

<?php
// render calendar now
$cArray = &new CreateQCalendarArray($m, $y);
$cal = &new QCalendar($cArray, $today, $links, $css);
if (!isset($_GET['ran'])) {
echo "<div id='quickCalender'>";
}
$cal->render();
if (!isset($_GET['ran'])) {
echo "</div>";
}
?>

 

Can anyone help? I've been bashing my head against the wall for days trying to get this to work. I emailed the author of the script and he hasn't responded. I'm sure the answer is right there I'm just missing it. Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/106622-help-with-event-calendar/
Share on other sites

Thanks for your input. The code from the tutorial places the events in an array called $links, and when I do a print_r($links) I see all of the events for that month. The problem I'm having is getting those to populate the date squares in the calendar. I've done something like this:

while ($row = mysql_fetch_array($result)) {
$pt_link = $row["link"];
$pt_name =  $row["name"]; 
$list_m.= "<a href=\"$pt_link\">$pt_name</a><br />";
}

and it listed multiple events per day, but it did a strange thing where it would keep the events from the previous day as part of the list. For example, the 15th of the month showed Event A and Event B. The 20th of the month only has Event C attached in the database, but showing up on the calendar was Event A, Event B, and Event C. It did this for the whole month but reset for the next month. Any thoughts?

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.