Trouble building calendar

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ITGeek
Forum Newbie
Posts: 3
Joined: Wed Aug 04, 2010 9:42 am

Trouble building calendar

Post by ITGeek »

I am having a problem trying to build a calendar that is designed to keep track of courses which includes day of week, date, time of day, spanning time of day and multiple courses being held in one day. I have built a basic monthly view calendar for listing podcasts, but that one doesn't have to have time of day built into each day of the week so it works quite well since it can just grab all courses listed for a day and toss it into the day block for whatever particular date they fell on.

I shall do my best to copy enough code here to hopefully get my point across and see if anyone has any ideas on how to rememdy my situation. Thanks in advance for any and all help!


First I query my database, which is a single table and all info for each course is it's own row in the database using a primary key so courses from semester to semester with the same course number do not get confused.

The "term" will be built into the link one would click on from one of our other pages or if they will use a drop down to choose another semester. Then I query based on what "term" they want to view. I need to show from 8AM until about 10PM each day (but left out a lot of the later times for ease of reading).

Code: Select all

<?php
$requestedTerm = $_GET['term'];

$query = "SELECT ccID, ccBuilding, ccRoom, ccDepartment, ccCoursenumber, ccSection, cc0800, cc0830, cc0900, cc0930, cc1000, ccMonday, ccTuesday, ccWednesday, ccThursday, ccFriday, ccSaturday, ccSunday, ccTerm, ccStartdate, ccEnddate FROM cc_courses WHERE ccTerm= '$requestedTerm' ORDER BY ccID ASC";

$query_id = mysql_query("$query") or die(mysql_error());
   while ($row = mysql_fetch_row($query_id)) {
      $ccID = $row[0];
				$ccBuilding = $row[1];
				$ccRoom = $row[2];
				$ccDepartment = $row[3];
				$ccCoursenumber = $row[4];
				$ccSection = $row[5];
				$cc0800 = $row[6];
                                $cc0830 = $row[7];
                                $cc0900 = $row[7];
                                $cc0930 = $row[7];
                                $cc01000 = $row[7];
                                $ccMonday = $row[34];
				$ccTuesday = $row[35];
				$ccWednesday = $row[36];
				$ccThursday = $row[37];
				$ccFriday = $row[38];
				$ccSaturday = $row[39];
				$ccSunday = $row[40];
				$ccTerm = $row[41];
				$ccStartdate = $row[42];
				$ccEnddate = $row[43];
				$ccTime = $row[44];
    }
From there I build a basic table with headers across the top listing time of day and links for them to be able to look a month ahead and a month behind.

Code: Select all

?>
<table>
		<tr></tr>
	     <tr>
			<td class="prev" colspan="4"><?php echo $previous_link; ?></td>
	     	<td class="month" colspan="3"><?php echo $month_name . "  " . $year; ?></td>
	  		<td class="next" colspan="4"><?php echo $next_link; ?></td>
	     </tr>
	     <tr>
		     <th></th>				<th></th>
		     <th>8:00</th>		   	<th>8:30</th>		<th>9:00</th>		<th>9:30</th>		<th>10:00</th>	
	     </tr>
<?php
Now I try to populate the rows and columns with the data. Some of the terminology you see is referenced from my calendarfunctions.php file which is an include in this file. (the else echo "no" is for testing to see what shows and what doesn't)

Code: Select all

foreach($days AS $day) { 
?>			
		<tr>
<?php
		foreach($day AS $dates) {
			
			$date_to_match = date("m-d-Y", mktime(0,0,0,$month,$d,$year));
?>		
			<td><?php echo ($dayname); ?></td>
			<td><?php echo ($dates); ?></td>
			
			<td>
<?php 
 
	               					if ($cc0800 == "1") { 
		               					echo ($ccDepartment . "." . $ccCoursenumber);
		               					 } else {
			               					echo " no ";
	               					} ?></td><td><?php
	               					if ($cc0830 == "1") { 
		               					echo ($ccDepartment . "." . $ccCoursenumber);
		               					 } else {
			               					echo " no ";
	               					} ?></td><td><?php
	               					if ($cc0900 == "1") { 
		               					echo ($ccDepartment . "." . $ccCoursenumber);
		               					 } else {
			               					echo " no ";
	               					} ?></td><td><?php
	               					if ($cc0930 == "1") { 
		               					echo ($ccDepartment . "." . $ccCoursenumber);
		               					 } else {
			               					echo " no ";
	               					} ?></td><td><?php
	               					if ($cc1000 == "1") { 
		               					echo ($ccDepartment . "." . $ccCoursenumber);
		               					 } else {
			               					echo " no ";
	               					} ?></td><td>
			
<?php
	} 
?>			
		</tr>
<?php
		
	}
	
?>
		</table>
	</form>
<?php mysql_close(); ?>
My one issue is that I have 3 courses in my database (3 rows of data total) and it is only finding the last in the array and working with it. The second, and more major issue, is that instead of looping row by row to see what falls on what date it is filling in each row in the calendar with the same info. Below is an image of the end result.

http://web1.johnshopkins.edu/classrooms ... Result.jpg

Thanks again for reading this far and for any help or suggestions you may have!
Faithe
Forum Commoner
Posts: 33
Joined: Tue Jul 12, 2005 3:26 pm
Location: WA

Re: Trouble building calendar

Post by Faithe »

I would do it like this. I only used three columns for brevity.

Code: Select all

<?php
$requestedTerm = $_GET['term'];
$query = ("SELECT * FROM cc_courses WHERE ccTerm = '$requestedTerm' ORDER BY ccID ASC"); ?>
<table>
<th>ID</th> 
<th>Room # </th>  
<th>8:00</th>
<?php WHILE ($result = mysql_fetch_array($query)) { ?>
<tr><td> <?php echo $result['ccID']; ?>
<td> <?php echo $result['ccRoom']; ?>
<td> <?php echo $result['cc0800']; ?>
<?php } ?>
</table>
 
ITGeek
Forum Newbie
Posts: 3
Joined: Wed Aug 04, 2010 9:42 am

Re: Trouble building calendar

Post by ITGeek »

I will give that a shot and post results back in the morning.
ITGeek
Forum Newbie
Posts: 3
Joined: Wed Aug 04, 2010 9:42 am

Re: Trouble building calendar

Post by ITGeek »

Most definitely a cleaner code! I rearranged a little bit and am getting a little closer, but the problem is lining up the correct course, on the day(s) of the week it falls along with having multiple courses in the same day fall in the same row (each row is a day of the week). Here is the cleaned up code with some adjustments to it and an image link to what we currently use, which is 100% manual input that we are trying to get into a php format so we can easily enter the course info into a database.

Code: Select all

$Bldg = $_GET['bldg'];
$Room = $_GET['room'];
$query = ("SELECT * FROM cc_courses WHERE ccBuilding= '$Bldg' AND ccRoom= '$Room' ORDER BY ccID ASC");
?>

<table>
<th>Day</th>
<th>Date</th>  
<th>8:00</th>
<th>8:30</th>
<th>9:00</th>
<th>9:30</th>
<th>10:00</th>

<?php 
$result = mysql_query($query) or die(mysql_error());
WHILE ($row = mysql_fetch_array($result)) { ?>

<tr>
<td><?php echo ($day_link); ?></td>
<td><?php echo ($days); ?></td>
<td><?php if ($row['cc0800'] == "1") { echo ($row['ccCoursenumber']); } ?></td>
<td><?php if ($row['cc0830'] == "1") { echo ($row['ccCoursenumber']); } ?></td>
<td><?php if ($row['cc0900'] == "1") { echo ($row['ccCoursenumber']); } ?></td>
<td><?php if ($row['cc0930'] == "1") { echo ($row['ccCoursenumber']); } ?></td>
<td><?php if ($row['cc1000'] == "1") { echo ($row['ccCoursenumber']); } ?></td>
</tr>

<?php 
} ?> 
</table>

<?php mysql_close(); ?>
http://web1.johnshopkins.edu/classrooms/sampleCal.jpg

Thanks again for any suggestions!!!
Post Reply