DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Michael has posted 27 posts at DZone. View Full User Profile

Large Calendar

12.14.2006
| 810 views |
  • submit to reddit
        // Large calendar, displays data from db, click day link for list of events for that particular day

CREATE TABLE `calendar` (
  `EventID` int(11) NOT NULL auto_increment,
  `EntryDate` date default NULL,
  `Type` text,
  `Details` text,
  `Event` varchar(15) default NULL,
  `Name` varchar(50) default NULL,
  `entered_by` varchar(30) NOT NULL,
  `entered_timestamp` datetime NOT NULL,
  `active` int(1) NOT NULL default '1',
  PRIMARY KEY  (`EventID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=191 ;



<?		
	$sql = "SELECT date_format(EntryDate, '%Y-%c-%e') as CalDate FROM calendar";
$rsCalDates = mysql_query($sql) or print ("Can't select entries from table entry.<br />" . $sql . "<br />" . mysql_error());

//This gets todays date
$date =time ();

//This puts the day, month, and year in seperate variables
$day = date('d', $date); 
$month = date('m', $date); 
$year = date('Y', $date);

//Added MJL

if (isset($_GET['m']) && isset($_GET['y']) && isset($_GET['d'])){ 
$month = $_GET['m'];
$year = $_GET['y'];
$day = $_GET['d'];
} else {
$dateComponents = getdate();
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
$day = $dateComponents['mday'];

}

//END MJL

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year); 

//This get's us the month name
$title = date('F', $first_day); 

//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day); 

//Once we know what day of the week it falls on, we know how many blank days occure before it.  If the first day of the week is a Sunday then it would be zero
switch($day_of_week){ 
    case "Sun": $blank = 0; break; 
    case "Mon": $blank = 1; break; 
    case "Tue": $blank = 2; break; 
    case "Wed": $blank = 3; break; 
    case "Thu": $blank = 4; break; 
    case "Fri": $blank = 5; break; 
    case "Sat": $blank = 6; break; 
}


//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year); 



//ADD MJL

if (mysql_num_rows($rsCalDates) > 0){
					mysql_data_seek($rsCalDates,0);
					while($row_rsCalDates = mysql_fetch_assoc($rsCalDates)){
						$dates[] = $row_rsCalDates['CalDate'];
						//$eventname[] = $row_rsCalDates['EventName'];
					}
				}

//END


//Added MJL
/* Computing the previous month. */
				if($month == 1) {
				 	$mn=12;
				 	$yn=$year-1;
					//$dy=31;
					$dy=1;
			 	} else {
				 		$mn=$month-1;
				 		$yn=$year;
						$dy=1;
				}
				
				/* Computing the next month. */
				if($month == 12) {
					$mn2=1;
					$yn2=$year+1;
					$dy=1;
				} else {
					$mn2=$month+1;
					$yn2=$year;
					$dy=1;
				}
//End add


//Here we start building the table heads
//echo "<table border=1 width=500 height=500 cellpadding=5 cellspacing=0>";
echo "<table id=calendar>";
echo "<tr><th colspan=7><center><br /><a href=index.php?cmd=home&m=$mn&y=$yn&d=$dy><<</a> $title $year <a href=index.php?cmd=home&m=$mn2&y=$yn2&d=$dy>>></a></center><br /></th></tr>";
echo "<tr class=day_row><td class=cal_td width=91><div align=center>Sun</div></td><td class=cal_td width=91><div align=center>Mon</div></td><td class=cal_td width=91><div align=center>Tues</div></td><td class=cal_td width=91><div align=center>Wed</div></td><td class=cal_td width=91><div align=center>Thurs</div></td><td class=cal_td width=91><div align=center>Fri</div></td><td class=cal_td width=91><div align=center>Sat</div></td></tr>";


//This counts the days in the week, up to 7
$day_count = 1;


echo "<tr>";

//first we take care of those blank days
while 
( $blank > 0 ) 
{ 
echo "<td></td>"; 
$blank = $blank-1; 
$day_count++;
} 


//sets the first day of the month to 1
$day_num = 1;

//count up the days, untill we've done all of them in the month
while 
( $day_num <= $days_in_month ) 
{ 
//add mjl

//$calendar .= "<td>$currentDay</td>";
						$date = $year."-".$month."-".$day_num;
						//$date2 = $year."-".$month."-".$day;
						
						if (in_array($date,$dates))
						{
						//echo $date;
						$sql2 = "SELECT Event,Name,Type,Details FROM calendar WHERE EntryDate = '$date' ORDER BY entered_timestamp ASC";//date_format(EntryDate, '%Y-%c-%e') as CalDate FROM calendar";
						$rsEventName = mysql_query($sql2) or print ("Can't select entries from table entry.<br />" . $sql2 . "<br />" . mysql_error());

						//echo mysql_result($rsEventName,$count,"EventName");
						
						
						if ($date == date('Y-m-d'))
						{
							echo "<td class=day_cell bgcolor=#FFFF00 valign=top><strong><a href=viewday.php?m=$month&y=$year&d=$day_num>$day_num</a></strong><br />";
						}
						else
						{
							echo "<td class=day_cell valign=top><strong><a href=viewday.php?m=$month&y=$year&d=$day_num>$day_num</a></strong><br />";
						}
						
						
						//echo "<td class=day_cell valign=top>";
						//echo "<strong><a href=viewday.php?m=$month&y=$year&d=$day_num>$day_num</a></strong><br />";
						
						//echo "";
							if (mysql_num_rows($rsEventName) > 0)
							{
									mysql_data_seek($rsEventName,0);
									while($row_rsEventName = mysql_fetch_assoc($rsEventName))
									{
									$Event = $row_rsEventName['Event'];
									$Name = $row_rsEventName['Name'];
									$Type= $row_rsEventName['Type'];
									$Details= $row_rsEventName['Details'];
									$last_name = explode(" ", $Name);
									//echo "day is " . $day_num;
									if ($Event == "")
									{
									//echo $row_rsEventName['EventName'];
									
									?>
										
									
									<!--<a href="mouseover.php?<?echo "m=".$month."&y=".$year."&d=".$day_num."&name=".$Name;?>" 
									style="text-decoration:none"
									class="term"
									onmouseover="var w=window.open(this.href, 'popup', 
									'width=400,height=100,scrollbars,resizable'); w.focus(); return false;">-->
									• <SPAN title=" <? echo $Name ." - ".$Type." - ".$Details; ?>" class="popup"><?echo ""  . $last_name[1];?></SPAN>
									</br>
									<?
									}
									else
									{
									?>
									• <SPAN title=" <? echo $Event ." - ".$Type." - ".$Details; ?>" class="popup"><?echo "" . $Event . "<br />";?></SPAN>
									<?
									}
									
									}
								//echo "<td valign=top><a href=index.php?cmd=dayview&m=$month&y=$year&d=$day_num>$day_num</a>$EventName</td>";
									
							}
						
						echo "<br /></td>";
						 
						//echo "<td valign=top><a href=index.php?cmd=dayview&m=$month&y=$year&d=$day_num>$day_num</a></td>"; 
						} else {
						
							if ($date == date('Y-m-d'))
							{
								echo "<td class=day_cell bgcolor=#FFFF00 valign=top><strong>$day_num</strong><br /><br /><br /><br /></td>"; 
							}
							else
							{
								echo "<td class=day_cell valign=top><strong>$day_num</strong><br /><br /><br /><br /></td>";
							}
						
						
						}

//end mjl
//echo "<td valign=top><a href=index.php?cmd=dayview&m=$month&y=$year&d=$day_num>$day_num</a></td>"; 
$day_num++; 
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}

} 


//Finaly we finish out the table with some blanks if needed
while 
( $day_count >1 && $day_count <=7 ) 
{ 
echo "<td> </td>"; 

$day_count++;
}




echo "</tr></table>";




?>