Page 1 of 1

Need suggestion on design of a calendar[SOLVED]

Posted: Wed Aug 31, 2005 4:23 pm
by raghavan20
This works fine when you want to display the current month.
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'>&laquo;PREV</a>
			<?php echo strtoupper($this->findMonth(1, $this->month, $this->year));?>
			&nbsp;&nbsp; <?php echo $this->year; ?>
			<a href='<?php echo $_SERVER["PHP_SELF"]; ?>?action=nextMonth'>NEXT&raquo;</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();
	}
}
?>

Posted: Wed Aug 31, 2005 4:29 pm
by feyd
instead of passing "previousMonth" pass the actual year and month to use. :)

Posted: Wed Aug 31, 2005 4:59 pm
by josh
If you want to have a link that calls an increment method, and do not want to just pass the month like feyd said you could serialize the object and store it with the session.

Posted: Wed Aug 31, 2005 5:01 pm
by raghavan20
I implemented the idea of feyd, it works now...thanks to both of you guys

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);
		}
		//find the next and previous months and years
		$previousLinkData = array();
		$previousLinkData = $this->findPreviousMonthYear();
		$nextLinkData = array();
		$nextLinkData = $this->findNextMonthYear();

		$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"]; ?>?month=<?php echo $previousLinkData[0];?>&year=<?php echo $previousLinkData[1];?>'>
			&laquo;PREV</a>
			<?php echo strtoupper($this->findMonth(1, $this->month, $this->year));?>
			&nbsp;&nbsp; <?php echo $this->year; ?>
			<a href='<?php echo $_SERVER["PHP_SELF"]; ?>?month=<?php echo $nextLinkData[0];?>&year=<?php echo $nextLinkData[1];?>'>
			NEXT&raquo;</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 findPreviousMonthYear(){
		//changing a month may also affect year value
		$tempArray[0] = date("m", mktime(0, 0, 0, $this->month-1, 1, $this->year));
		$tempArray[1] = date ("Y", mktime(0, 0, 0, $this->month-1, 1, $this->year));
		return $tempArray;
	}
	
	function findNextMonthYear(){
		//changing a month may also affect year value
		$tempArray[0] = date("m", mktime(0, 0, 0, $this->month+1, 1, $this->year));
		$tempArray[1] = date ("Y", mktime(0, 0, 0, $this->month+1, 1, $this->year));
		return $tempArray;
	}
	
	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
//set the values for year and month
if (isset($_GET["year"]) && isset($_GET["month"])){
	$month = $_GET["month"];
	$year = $_GET["year"];
	if (!is_numeric($month) || $month < 1 || $month > 12){
		$year = date("m");
	}
	if (!is_numeric($year) || $year < 1900 || $year > 2050){
		$year = date("Y");
	}
}else{
	$year = date("Y");
	$month= date("m");
}

$calendar = new Calendar("hostname", "username", "password", "database", $year, $month);
$calendar->prepareCalendar("select * from Dates_tbl", "Calendar1.class.php?action=nothing&id=");
$calendar->renderCalendar();
?>

Posted: Wed Aug 31, 2005 5:03 pm
by raghavan20
jshpro2 wrote:If you want to have a link that calls an increment method, and do not want to just pass the month like feyd said you could serialize the object and store it with the session.
Even though i have done that...I would like to understand your idea.
Actually I could not understand wot you said; can you explain it for me. :)

Posted: Wed Aug 31, 2005 5:13 pm
by feyd
basically, you stick the object into your session array. You just need to make sure that the class is defined before you start the session up again or you'll get an error.

Posted: Wed Aug 31, 2005 5:43 pm
by raghavan20
alright, I have another problem now.
I have a database field which has mysql timestamp which I want to convert to normal timestamp used by PHP.
how do i do that???

Posted: Wed Aug 31, 2005 5:44 pm
by feyd
normal timestamp?

Posted: Wed Aug 31, 2005 5:46 pm
by raghavan20
thing are reversed
php unix timestamp: time followed by date
mysql timestamp:date followed by time
I want the mysql timestamp converted into php unix timestamp.

for ex:
mysql timestamp:20050830172550
if i say date("m", $theabove) it returns a wrong vaue which is not a month

look at here for more details:
http://raghavan20.allhyper.com/Calendar1.class.php

Posted: Wed Aug 31, 2005 6:06 pm
by feyd
fyi: php unix timestamp is an integer, not a datetime mark compacted ;)

Code: Select all

[feyd@home]>php -r "print_r(array_map('intval',unpack('A4year/A2month/A2day/A2hr/A2min/A2sec','20050830172550')));"
Array
(
    [year] => 2005
    [month] => 8
    [day] => 30
    [hr] => 17
    [min] => 25
    [sec] => 50
)