Page 1 of 1

Dynamically Change Month

Posted: Wed Sep 01, 2010 6:54 pm
by bradbury
I am working on a calendar program right now and have currently coded the whole thing out in OOP and it works great for displaying the month and all that business but now I am stuck trying to figure out how to change the month when a navigation button is pressed. I will post the code below:

Code: Select all

<?php
	class Calendar {
		
		public function createCalendar($id,$p) {
			$this->id = $id;
			$this->pix = $p;
			$this->days[0] = 'Sun';
			$this->days[1] = 'Mon';
			$this->days[2] = 'Tue';
			$this->days[3] = 'Wed';
			$this->days[4] = 'Thu';
			$this->days[5] = 'Fri';
			$this->days[6] = 'Sat';
			$this->months[1] = 'January';
			$this->months[2] = 'February';
			$this->months[3] = 'March';
			$this->months[4] = 'April';
			$this->months[5] = 'May';
			$this->months[6] = 'June';
			$this->months[7] = 'July';
			$this->months[8] = 'August';
			$this->months[9] = 'September';
			$this->months[10] = 'October';
			$this->months[11] = 'November';
			$this->months[12] = 'December';
			$this->month = date('n');
			$this->curmonth = date('n');
			$this->day = date('j');
			$this->year = date('Y');
			$this->daysInMonth = date("t", mktime(0,0,0, $this->month,1,$this->year));			
			$this->firstDay = date("w", mktime(0,0,0,$this->month,1,$this->year));

			$this->write = $this->writeCalendar();
		}
		public function getFormattedDate() {
			$days = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
			$months = array('January','February','March','April','May','June','July','August','September','October','November','December');
		}
		public function writeCalendar() {
			echo "<div id=\"" . $this->pix . "\">";
			echo "<table id=\"cal" . $this->id . "\" cellspacing=\"0\" width=\"200\" style=\"border:0;\">";
			echo "<tr><th colspan=\"7\" class=\"month\">" . $this->months[$this->month] . ", " . $this->year . "</th></tr>";
			echo "<tr>";
			
			for ($i=0; $i < 7; $i++) {
			echo "<th class=\"dayHeader\">" . $this->days[$i] . "</th>";
			}
			echo "<tr>";
			for($j=0;$j<42;$j++){
			$this->displayNum = ($j-$this->firstDay+1);
			if($j<$this->firstDay){
				//write the leading empty cells
				echo "<td class=\"empty\">&nbsp;</td>";
			}else if($this->displayNum==$this->day && $this->month == $this->curmonth){
				echo "<td id=\"" . $this->id . "selected\" class=\"date\">" . $this->displayNum . "</td>";
			}else if($this->displayNum > $this->daysInMonth){
				//Empty cells at bottom of calendar
				echo '<td>&nbsp;</td>';
			}else if($this->displayNum == $this->day && $this->month == $this->curmonth){
				echo "<td id=\"" . $this->id . "selected\" class=\"event\">" . $this->displayNum . "</td>";
			}else{
				//the rest of the numbered cells
				echo '<td id=\"\" class=\"days\">' . $this->displayNum . '</td>';
			}
			if($j%7==6){
				echo '</tr><tr>';
			}
			}
			echo '</tr>';
			//write the nav row THIS IS WHERE I WANTED TO PUT THE CODE IN TO NAVIGATE THE CALENDAR
			echo '<tr>';
			echo '<td class=\"nav\" style=\"text-decoration:underline;\" onClick=\"changeMonth(-12,\'' . $this->id . '\', \'' . $this->pix . '\')\"><</td>';
			echo '<a href=\"<td class=\"nav\"><</td>';
			echo '<td class=\"month\" colspan=\"3\">&nbsp;</td>';
			echo '<td class=\"nav\" onClick=\"changeMonth(1,\'' . $this->id . '\', \'' . $this->pix . '\')\">></td>';
			echo '<td class=\"nav\" style=\"text-decoration:underline;text-align:right;\" onClick=\"changeMonth(12,\'' . $this->id . '\', \'' . $this->pix . '\')\">></td>';
			echo '</tr>';
			
			echo '</table>';
			echo '</div>';
		}
		public function changeMonth() {
			
		}
}
?>
I know that I may have to implement some type of ajax but I'm not too familiar how to do that with OOP.

Re: Dynamically Change Month

Posted: Wed Sep 01, 2010 11:06 pm
by requinix
Forget AJAX for the moment. That should be a feature, not a requirement.

Use links like calendar.php?year=2010&month=9, then look to $_GET for which date to use (if any). Then write your changeMonth to accept a year and month...

Re: Dynamically Change Month

Posted: Thu Sep 02, 2010 11:08 am
by bradbury
I was going to do that but the only problem is that if I do use a $_GET call then won't I run into the issue if I have multiple calendars they will all switch when I hit the navigation for one of them ? or will php realize that the variables are unique to the object?

Re: Dynamically Change Month

Posted: Thu Sep 02, 2010 9:02 pm
by requinix
If you'll have multiple calendars on the same page then AJAX will be easier to implement, after all.

However, you still need something like what I mentioned: calendar.php which accepts $_GET["year"] and $_GET["month"]. Then it outputs the HTML fragment for the calendar.
Move the <div> out of writeCalendar() and into a normal script. So it'd be like

Code: Select all

echo "<div id='foo'>";
$calendar->writeCalendar();
echo "</div>";
After those little changes, get the base AJAX stuff in place. You can use jQuery/another library or code it yourself.

Re: Dynamically Change Month

Posted: Fri Sep 03, 2010 4:42 am
by internet-solution
If you want a pure PHP solution then you can have separate $_GET variables for each calendar. $_GET['year1'], $_GET['month1'] for calendar 1, etc.