what I want to do is, add next and previous months functionalities.
I dont know how to do this.
I have a Calendar class in a file.
I have got methods to increment and decrement current month.
somehow, when the user clicks on next and previous it shd call increment/decrement month methods and render after that.
I am sure that I have not explained this properly cos I am confused so bear with me guys
all suggestions are welcome.
file at action: http://raghavan20.allhyper.com/Calendar1.class.php
Code: Select all
<style type="text/css">
<!--
span {
height:10px;
width:40px;
float:left;
margin:1px;
text-align:center;
background-color:#333333;
color:#FFFFFF;
font-size:.8em;
font-weight:900;
line-height:1.2em;
border:1px solid #996666;
}
.mainDiv{
border:2px solid #000000;
padding:2px;
width:300px;
background-color:#CCCCCC;
}
.calendarTitle{
width:300px;
text-align:center;
font-weight:bold;
}
.links{
color:white;
text-decoration:underline;
}
-->
</style>
<?php
class Calendar{
var $month;
var $year;
var $dbConnection;
var $query;
var $redirectLink;
function Calendar($hostName, $userName, $password, $databaseName, $year="", $month=""){
if (empty($year)){//set current year if empty
$this->year = date("Y");
}else{
$this->year = $year;
}
if (empty($month)){//set current month if empty
$this->month= date("m");
}else{
$this->month = $month;
}
//connect to database
$this->dbConnection = mysql_pconnect($hostName, $userName, $password);
mysql_select_db($databaseName, $this->dbConnection);
}
function prepareCalendar($query, $redirectLink){
$this->query = $query;
$this->redirectLink = $redirectLink;
}
function renderCalendar(){
$tempArray = array(); //to store the set of dates
$counter = 0; //used to count the number of days displayed in a week
$result = $this->executeQuery($this->query);
if ($result){
$tempArray = $this->getDatesArray($result);
}
//print_r($tempArray);
$weekDay = $this->getWeekDay(1, $this->month, $this->year);//get the first weekday of the month
$daysOfMonth = $this->findDaysOfMonth(1, $this->month,$this->year); //get number of days in a year
?>
<!--display the month and year of the calendar--->
<div class="calendarTitle">
<a href='<?php echo $_SERVER["PHP_SELF"]; ?>?action=previousMonth'>«PREV</a>
<?php echo strtoupper($this->findMonth(1, $this->month, $this->year));?>
<?php echo $this->year; ?>
<a href='<?php echo $_SERVER["PHP_SELF"]; ?>?action=nextMonth'>NEXT»</a>
</div>
<!--display the month and the days--->
<div class = "mainDiv">
<div><span>SUN</span><span>MON</span><span>TUE</span><span>WED</span>
<span>THU</span><span>FRI</span><span>SAT</span></div>
<div>
<?php
//display blank spans
for ($i = 1; $i <= $weekDay; $i++){
echo "<span></span>";
}
$counter = $weekDay;//set the counter
//display all days of the month;
for ($i = 1; $i <= $daysOfMonth; $i++){
if (in_array($i, $tempArray)){//$tempArray holds all the dates; check for existance of array element
echo "<span><a href='".$this->redirectLink.$i.".' onclick='alert($i);' class='links'>$i</a></span>";
}else{
echo "<span>$i</span>";
}
$counter++;
if ($counter%7 == 0){//introduce a break for every week
echo "<br />";
}
}
?>
</div></div>
<?php
}
function decrementMonth(){
//changing a month may also affect year value
$this->month = date("m", mktime(0, 0, 0, $this->month-1, 1, $this->year));
$this->year = date ("Y", mktime(0, 0, 0, $this->month-1, 1, $this->year));
}
function incrementMonth(){
//changing a month may also affect year value
$this->month = date("m", mktime(0, 0, 0, $this->month+1, 1, $this->year));
$this->year = date ("Y", mktime(0, 0, 0, $this->month+1, 1, $this->year));
}
function getWeekDay($day, $month, $year){
$timestamp = mktime(0, 0, 0, $month, $day, $year);
$date = getdate($timestamp);
return $date["wday"];
}
function findDaysOfMonth($day=1, $month, $year){
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date("t", $timestamp);
}
function findMonth($day=1, $month, $year){
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date("F", $timestamp);
}
function executeQuery($query){
$result = mysql_query($query);
if (is_resource($result)){
return $result;
}else{
return FALSE;
}
}
function getDatesArray($result){
if (is_resource($result)){
if (mysql_num_rows($result) > 0){
$tempArray = array();
while($row = mysql_fetch_row($result)){
array_push($tempArray, $row[0]); //$row[0] assumed to be an individual date
}
}
}
if (is_array($tempArray) && !empty($tempArray)){
return $tempArray;
}else{
return FALSE;
}
}
function __destructor(){
}
}
?>
<?php
$calendar = new Calendar("hostname", "username", "password", "database name");
$calendar->prepareCalendar("select * from Dates_tbl", "Calendar1.class.php?action=nothing&id=");
$calendar->renderCalendar();
?>
<?
if (isset($_GET["action"])){
if ($_GET["action"] == "previousMonth"){
$calendar->decrementMonth();
$calendar->renderCalendar();
}
elseif($_GET["action"] == "nextMonth"){
$calendar->incrementMonth();
$calendar->renderCalendar();
}
}
?>