Hi, I have built a PHP calendar and added all the functionality I need to it except for one small thing. I need a "next" and "previous" button to be able to cycle through the months on the calendar. I already have a drop-down form to select the month and year, but i'd also like to be able to allow users to cycle through the months a little easier using a few simple buttons next to the date up the top.
For some reason though I just can't seem to get it working and i'm not sure what i'm doing wrong, i've tried a few things using PHP, html and JavaScript and can't seem to get it right. Any help would be appreciated with this.
<?php
define("ADAY", (60*60*24));
if ((!isset($_POST['month'])) || (!isset($_POST['year']))) {
$nowArray = getdate();
$month = $nowArray['mon'];
$year = $nowArray['year'];
} else {
$month = $_POST['month'];
$year = $_POST['year'];
}
$start = mktime(12,0,0,$month,1,$year);
$firstDayArray = getdate($start);
?>
<html>
<head>
<title><?php echo "Calendar: ".$firstDayArray['month']."" . $firstDayArray['year']; ?></title>
</head>
<body>
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<p style=\"margin-top: 0px; margin-left: 3px; margin-bottom: 5px;\">".$firstDayArray['month']." " .$firstDayArray['year']."</p>";
echo "<table border=\"1\" class=\"caltable\" cellpadding=\"4\"><tr>\n";
foreach ($days as $day) {
echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%; background-image: url(images/calendarheader.jpg); border-bottom: 2px solid #52789b;\">
<strong>$day</strong></td>\n";
}
for ($count=0; $count < (6*7); $count++) {
$dayArray = getdate($start);
if (($count % 7) == 0) {
if ($dayArray["mon"] != $month) {
break;
} else {
echo "</tr><tr>\n";
}
}
if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) {
echo "<td style=\"background-color: #EEEEEE\"> </td>\n";
} else {
$chkEvent_sql = "SELECT eventid FROM events WHERE month(event_date) = '".$month."' AND dayofmonth(event_date) = '".$dayArray["mday"]."' AND year(event_date) = '".$year."' ORDER BY event_date";
$chkEvent_res = mysql_query($chkEvent_sql, $mysql) or die(mysql_error($mysql));
if (mysql_num_rows($chkEvent_res) > 0) {
$event_name = "";
$event_format1 = "";
$event_format2 = "";
$event_format3 = "#FFFFFF";
if ($ev = mysql_fetch_array($chkEvent_res)) {
$event_name .= "</br>";
$event_format1 = "<b>";
$event_format2 = "</b>";
$event_format3 = "#DEDEDE";
}
mysql_free_result($chkEvent_res);
} else {
$event_name = "</br>";
$event_format1 = "";
$event_format2 = "";
$event_format3 = "#FFFFFF";
}
$today = date('Y-n-j');
$dateToCompare = $year. '-' . $month. '-' . $dayArray["mday"];
echo "<td style=\"background-color: $event_format3\"\"valign=\"top\">".$event_format1."<a ";
if ($today == $dateToCompare){
echo "class='today'";
}
echo "href=upcomingevents.php?m=".$month."&d=".$dayArray["mday"]."&y=".$year.">".$dayArray["mday"]."</a>".$event_format2."<br/>".$event_name."</td>\n";
unset($event_name);
$start += ADAY;
}
}
echo "</tr></table>";
mysql_close($mysql);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x<=count($months); $x++){
echo "<option value=\"$x\"";
if ($x == $month){
echo " selected";
}
echo ">".$months[$x-1]."</option>";
}
?>
</select>
<select name="year">
<?php
for ($x=2000; $x<=2020; $x++){
echo "<option";
if ($x == $year){
echo " selected";
}
echo ">$x</option>";
}
?>
</select>
<input type="submit" name="submit" value="Go!">
</form>
</body>
</html>