Dynamically Change Month
Posted: Wed Sep 01, 2010 6:54 pm
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:
I know that I may have to implement some type of ajax but I'm not too familiar how to do that with OOP.
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\"> </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> </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\"> </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() {
}
}
?>