[ create a new paste ] login | about

Link: http://codepad.org/28XT71AB    [ raw code | output | fork ]

PHP, pasted on Apr 2:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>University Timetable</title>
<link rel="stylesheet" href="style.css" />

<script type="text/javascript" src="popup.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</script>

</head>
<body>

<!--start PHP code-->
<?php



//REMOVE ME
ini_set('display_errors',1);
error_reporting(E_ALL);

/*
  variables that work out current week
*/

$termstart = strtotime("03 October 2011"); //start of term, set manually every year, week 1 is first week after freshers.
$todaysdate = strtotime("now"); //todays date
$weekdif = ceil(($todaysdate-$termstart)/604800);//weeks between the two dates
//define global variables and arrays
$row = 0;
$col = 0;
$num = 0;


//this is the main timetable interface array
$val = array(array());

$ttdata = array();

$lecs = array();

tableinit($weekdif);

function tableinit($wkd)
{

	$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
	$times = array("09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00");


	//Check for appropriate CSV file and open it
	if (($handle = fopen("timetable.csv", "r")) !== FALSE)
	{
		//Check file for data and copy it into an array
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
		{
			//filter out blank lines in the file
			$fdata = array_filter($data);
			$num = count($fdata);

			//If line is not empty
			if ($num > 0)
			{
				//for every value in the array (the line)
				for ($c=0; $c<$num; $c++)
				{

					/*
					  This gets the module code, trims it of useless data,
					  then adds the name into an array of lectures for comparison later.
					  This is used to set up different colours for each different module
					*/

					if ($c == 3)
					{
						$lecture = substr($fdata[$c],0,8);

						if (empty($lecs))
						{
							$lecs[] = $lecture;
						}
						else if (!(in_array($lecture,$lecs)))
						{
							$lecs[] = $lecture;
						}

						$ttdata[] = $lecture;
					}

					//if it's the 4th value or higher, then its data we want to display.
					if ($c >= 4)
					{
						//add the data to an array. If no array exists, create it
						$ttdata[] = $fdata[$c];
					}



					/*
					  if the value is a day of the week
					  set the value of the first timetable column as the appropriate day
					  with the corresponding row
					*/

					switch ($fdata[$c])
					{
						case $days[0]:
							$row = 0;
							$val[$row][0] = $fdata[$c];
							break;
						case $days[1]:
							$row = 1;
							$val[$row][0] = $fdata[$c];
							break;
						case $days[2]:
							$row = 2;
							$val[$row][0] = $fdata[$c];
							break;
						case $days[3]:
							$row = 3;
							$val[$row][0] = $fdata[$c];
							break;
						case $days[4]:
							$row = 4;
							$val[$row][0] = $fdata[$c];
							break;
						case $days[5]:
							$row = 5;
							$val[$row][0] = $fdata[$c];
							break;
						case $days[6]:
							$row = 6;
							$val[$row][0] = $fdata[$c];
							break;
					}

					/*
					  this function compares the current week to the weeks in the timetable.
					  If there's a match, add a flag to the array for that lecture.
					  if not, do nothing.
					*/

					if ($c == 6)
					{
						$exp1 = explode(",", $fdata[$c]);

						foreach ($exp1 as $i)
						{
							$i = trim($i);
							$exp2 = explode("-", $i);

							if (($wkd >= $exp2[0])&&($wkd <= $exp2[1]))
							{
								$ttdata[] = TRUE;
							}
						}
					}

					/*
					  if c the second value in the array,
					  check the value against the Time array
					  and set the column appropriately
					*/
					if ($c==1)
					{
						switch ($fdata[$c])
						{
							case $times[0]:
								$col = 1;
								break;
							case $times[1]:
								$col = 2;
								break;
							case $times[2]:
								$col = 3;
								break;
							case $times[3]:
								$col = 4;
								break;
							case $times[4]:
								$col = 5;
								break;
							case $times[5]:
								$col = 6;
								break;
							case $times[6]:
								$col = 7;
								break;
							case $times[7]:
								$col = 8;
								break;
							case $times[8]:
								$col = 9;
								break;
						}
					}
				} //end line

				//fill the timetable with whitespace to preserve shape and empty slots
				for ($i=0;$i<=6;$i++)
				{
					for ($j=1;$j<=9;$j++)
					{
						if (!isset($val[$i][$j]))
						{
							$val[$i][$j] = "&nbsp;";
						}
					}
				}

				//if there's a flag to display data
				if (isset($ttdata[4]))
				{
					//remove the flag
					unset($ttdata[4]);
					//fill the current timetable position with the array of data to display
					$val[$row][$col] = $ttdata;
				}

				//delete the array of data to display
				unset($ttdata);
			}
		}
		fclose($handle);//close the file when finished
	}

}

drawtable($weekdif, $val);

function drawtable($wkd, $val)
{

	$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
	$times = array("09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00");


	//this sets up any days with no lectures, so there's not just a blank line
	for ($m=0;$m<5;$m++)
	{
		$row=$m;
		$val[$row][0] = $days[$m];
	}

	//create the table for the data from the main array
	$table = "<table class='main' align='center'>\n<tr><td>Week $wkd</td>";

	//create the line of different lecture times in the first row of the table
	foreach ($times as $t)
	{
		$table .= "<td class='dt'> ". $t . "</td>";
	}
	$table .= "</tr>\n"; //end first row

	//for every weekday
	for ($i=0;$i<5;$i++)
	{
		//create a new row
		$table .= "<tr>";

		//for every time slot on that day
		for ($j=0;$j<=9;$j++)
		{
			//if there's an array present
			if (is_array($val[$i][$j]) == TRUE)
			{
				//copy the array to a temporary one
				$temp = $val[$i][$j];

				/*
				  Switch statement to ensure that each module is always shown as a different colour.
				  the same module will always be the same colour. Different modules will always be different colours.
				*/

				switch ($temp[0])
				{
					case $lecs[0]:
						$table .= "<td class='lecture1'>";
						break;
					case $lecs[1]:
						$table .= "<td class='lecture2'>";
						break;
					case $lecs[2]:
						$table .= "<td class='lecture3'>";
						break;
					case $lecs[3]:
						$table .= "<td class='lecture4'>";
						break;
					case $lecs[4]:
						$table .= "<td class='lecture5'>";
						break;
					case $lecs[5]:
						$table .= "<td class='lecture6'>";
						break;
				}
				//for each value in the array
				foreach ($temp as $datum)
				{
					//print it and create a new line
					$table .= " ". $datum . " <br />";
				}
				$table .= "</td>";
			}
			//otherwise if a Day is present
			elseif ($j==0)
			{
				//print it
				$table .= "<td class='dt'>";
				$table .= $val[$i][$j];
				$table .= "</td>";
			}
			//otherwise
			else
			{
				//print the whitespace
				$table .= "<td class='tt'>";
				$table .= $val[$i][$j];
				$table .= "</td>";
			}

		}
		//end row
		$table .= "</tr>\n";
	}
	//end table
	$table .= "</table>";

	//print the entire table
	echo $table;

}

$buttontext = "Show Next Week";

function weekchange($buttontext)
{

	if ($buttontext == "Show Next Week")
	{
		nextweek();
	}
	else
	{
		thisweek();
	}
}

function nextweek()
{
	$todaysdate = strtotime("now + 7 days");
	$termstart = strtotime("03 October 2011");
	$buttontext = "Hide Next Week";
	tableinit($todaysdate, $termstart);
}

/*function thisweek()
{
	$todaysdate = strtotime("now");
	$termstart = strtotime("03 October 2011");
	$buttontext = "Next Week";
	tableinit($todaysdate, $termstart);
}*/

if (isset($_POST['nextw']))
{
	weekchange($buttontext);
}
?>
<!--end PHP code-->

<table align="center">
<tr>
<form method="post" action="tt.php">
<input type="hidden" name="nextw" value="true">
<td><input name="submit" type="Submit" value="<?php echo $buttontext ?>"></td>
</form>

<td><button type="button" style="button" onClick="fireNlPopup()">Next Lecture</button></td>
<td><button type="button" style="button" onClick="window.print()">Print</button></td>
<td><button type="button" style="button">Accessibility</button></td>
</tr>
</table>

<div id="NlPopup" name="NlPopup" class="nl">
<table width="380" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><img height="23" width="356" src="images/x11_title.gif"></td>
<td><a href="javascript:fadeOutNlPopup();"><img height="23" width="24" src="images/x11_close.gif" border="0"></a></td>
</tr>
<tr><td colspan="2" style="background: url("images/x11_body.gif") no-repeat top left; width: 380px; height: 277px;">
Your next lecture is X, with Y, at Z.
</td></tr>
</table>
</div>



</body>
</html>


Output:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>University Timetable</title>
<link rel="stylesheet" href="style.css" />

<script type="text/javascript" src="popup.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</script>

</head>
<body>

<!--start PHP code-->

Warning: fopen(timetable.csv): failed to open stream: No such file or directory on line 54

Notice: Undefined offset:  1 on line 267

Notice: Undefined offset:  1 on line 319

Notice: Undefined offset:  2 on line 267

Notice: Undefined offset:  2 on line 319

Notice: Undefined offset:  3 on line 267

Notice: Undefined offset:  3 on line 319

Notice: Undefined offset:  4 on line 267

Notice: Undefined offset:  4 on line 319

Notice: Undefined offset:  5 on line 267

Notice: Undefined offset:  5 on line 319

Notice: Undefined offset:  6 on line 267

Notice: Undefined offset:  6 on line 319

Notice: Undefined offset:  7 on line 267

Notice: Undefined offset:  7 on line 319

Notice: Undefined offset:  8 on line 267

Notice: Undefined offset:  8 on line 319

Notice: Undefined offset:  9 on line 267

Notice: Undefined offset:  9 on line 319

Notice: Undefined offset:  1 on line 267

Notice: Undefined offset:  1 on line 319

Notice: Undefined offset:  2 on line 267

Notice: Undefined offset:  2 on line 319

Notice: Undefined offset:  3 on line 267

Notice: Undefined offset:  3 on line 319

Notice: Undefined offset:  4 on line 267

Notice: Undefined offset:  4 on line 319

Notice: Undefined offset:  5 on line 267

Notice: Undefined offset:  5 on line 319

Notice: Undefined offset:  6 on line 267

Notice: Undefined offset:  6 on line 319

Notice: Undefined offset:  7 on line 267

Notice: Undefined offset:  7 on line 319

Notice: Undefined offset:  8 on line 267

Notice: Undefined offset:  8 on line 319

Notice: Undefined offset:  9 on line 267

Notice: Undefined offset:  9 on line 319

Notice: Undefined offset:  1 on line 267

Notice: Undefined offset:  1 on line 319

Notice: Undefined offset:  2 on line 267

Notice: Undefined offset:  2 on line 319

Notice: Undefined offset:  3 on line 267

Notice: Undefined offset:  3 on line 319

Notice: Undefined offset:  4 on line 267

Notice: Undefined offset:  4 on line 319

Notice: Undefined offset:  5 on line 267

Notice: Undefined offset:  5 on line 319

Notice: Undefined offset:  6 on line 267

Notice: Undefined offset:  6 on line 319

Notice: Undefined offset:  7 on line 267

Notice: Undefined offset:  7 on line 319

Notice: Undefined offset:  8 on line 267

Notice: Undefined offset:  8 on line 319

Notice: Undefined offset:  9 on line 267

Notice: Undefined offset:  9 on line 319

Notice: Undefined offset:  1 on line 267

Notice: Undefined offset:  1 on line 319

Notice: Undefined offset:  2 on line 267

Notice: Undefined offset:  2 on line 319

Notice: Undefined offset:  3 on line 267

Notice: Undefined offset:  3 on line 319

Notice: Undefined offset:  4 on line 267

Notice: Undefined offset:  4 on line 319

Notice: Undefined offset:  5 on line 267

Notice: Undefined offset:  5 on line 319

Notice: Undefined offset:  6 on line 267

Notice: Undefined offset:  6 on line 319

Notice: Undefined offset:  7 on line 267

Notice: Undefined offset:  7 on line 319

Notice: Undefined offset:  8 on line 267

Notice: Undefined offset:  8 on line 319

Notice: Undefined offset:  9 on line 267

Notice: Undefined offset:  9 on line 319

Notice: Undefined offset:  1 on line 267

Notice: Undefined offset:  1 on line 319

Notice: Undefined offset:  2 on line 267

Notice: Undefined offset:  2 on line 319

Notice: Undefined offset:  3 on line 267

Notice: Undefined offset:  3 on line 319

Notice: Undefined offset:  4 on line 267

Notice: Undefined offset:  4 on line 319

Notice: Undefined offset:  5 on line 267

Notice: Undefined offset:  5 on line 319

Notice: Undefined offset:  6 on line 267

Notice: Undefined offset:  6 on line 319

Notice: Undefined offset:  7 on line 267

Notice: Undefined offset:  7 on line 319

Notice: Undefined offset:  8 on line 267

Notice: Undefined offset:  8 on line 319

Notice: Undefined offset:  9 on line 267

Notice: Undefined offset:  9 on line 319
<table class='main' align='center'>
<tr><td>Week 27</td><td class='dt'> 09:00</td><td class='dt'> 10:00</td><td class='dt'> 11:00</td><td class='dt'> 12:00</td><td class='dt'> 13:00</td><td class='dt'> 14:00</td><td class='dt'> 15:00</td><td class='dt'> 16:00</td><td class='dt'> 17:00</td></tr>
<tr><td class='dt'>Monday</td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td></tr>
<tr><td class='dt'>Tuesday</td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td></tr>
<tr><td class='dt'>Wednesday</td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td></tr>
<tr><td class='dt'>Thursday</td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td></tr>
<tr><td class='dt'>Friday</td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td><td class='tt'></td></tr>
</table><!--end PHP code-->

<table align="center">
<tr>
<form method="post" action="tt.php">
<input type="hidden" name="nextw" value="true">
<td><input name="submit" type="Submit" value="Show Next Week"></td>
</form>

<td><button type="button" style="button" onClick="fireNlPopup()">Next Lecture</button></td>
<td><button type="button" style="button" onClick="window.print()">Print</button></td>
<td><button type="button" style="button">Accessibility</button></td>
</tr>
</table>

<div id="NlPopup" name="NlPopup" class="nl">
<table width="380" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><img height="23" width="356" src="images/x11_title.gif"></td>
<td><a href="javascript:fadeOutNlPopup();"><img height="23" width="24" src="images/x11_close.gif" border="0"></a></td>
</tr>
<tr><td colspan="2" style="background: url("images/x11_body.gif") no-repeat top left; width: 380px; height: 277px;">
Your next lecture is X, with Y, at Z.
</td></tr>
</table>
</div>



</body>
</html>


Create a new paste based on this one


Comments: