Skip to content

Instantly share code, notes, and snippets.

@MarkyC
Created January 24, 2013 00:21
Show Gist options
  • Select an option

  • Save MarkyC/4616216 to your computer and use it in GitHub Desktop.

Select an option

Save MarkyC/4616216 to your computer and use it in GitHub Desktop.
<?php
// get first row, and number of rows
// we have to keep track so we don't mysql_fetch_row() more than we can
$row = mysql_fetch_row($result);
$num_rows = mysql_num_rows($result);
$current_row = 1;
// grab info for the first class
$name = $row[0];
$description = $row[1];
$location = $row[2];
$url = $row[3];
$day = $row[4];
$start_time = $row[5];
$end_time = $row[6];
// Set earliest start and latest end times
// This is used to hide rows for times the user doesn't have classes
$earliest_start_time = $start_time;
$latest_end_time = $end_time;
$done = false; // used to "show more" after the last class is plotted
$skipcell = 0; // skip no cells
// hide all classes before $start_time of first class
$time = mktime(0, 0, 0, 1, 1);
for ($i = 0; $i < 86400; $i += 1800) { // 1800 = half hour, 86400 = one day
echo "<tr ";
if ($i < ($start_time - 1800)) {
// initially hide time slots before start time of first class
echo "class=\"initiallyHiddenTop\" ";
} else if ($i == ($start_time - 1800)) {
// show "Show More" at 30 mins before class start
// (because the user should be able to see the start time of their class)
echo "onClick=\"unHideTop()\"><th class=\"show-more\" id=\"show-more-top\" " .
"style=\"text-align:center\" colspan=\"8\">" .
"<i class=\"icon-double-angle-up\"></i> Show More " .
"<i class=\"icon-double-angle-up\"></i></th></tr>";
continue;
} else if (($done) && ($i == ($latest_end_time + 1800))) {
// Show "Show More" at 30 mins after last class ends
echo "onClick=\"unHideBottom()\"><th class=\"show-more\" id=\"show-more-bottom\" " .
"style=\"text-align:center\" colspan=\"8\">" .
"<i class=\"icon-double-angle-down\"></i> Show More " .
"<i class=\"icon-double-angle-down\"></i></th></tr>";
continue;
} else if (($done) && ($i > ($latest_end_time + 1800))) {
// initially hide time slots after end time of last class
echo "class=\"initiallyHiddenBottom\" ";
}
// output header
printf('><th style="text-align:right">%1$s</th>',
date('g:i a', $time + $i));
// output cell for each day of week
for ($j = 1; $j < 8; $j++) { // sunday = 1...
if (($i == $start_time) && ($day == $j) && (!$done)) {
// there is a class today at this time
// get class duration
// [end_time (seconds) - start_time (seconds)] / 1800 (seconds/half hr)
// ex 9am - 8am / 1800secs
// = (32400 - 28800) / 1800 = 2
$row_span = ($end_time - $start_time) / 1800;
$skipcell = $row_span - 1;
// print class info
echo "<td class=\"non-empty-cell\">" .
"<p><abbr class=\"class-name\" title=\"$description\">$name</abbr> " .
"<a href=\"$url\"><i class=\"icon-link\"></i></a></p>" .
"<p>$location</p>" .
"</td>";
// iterate forwards through results
if ($current_row <= $num_rows) {
// There is more classes to plot
$current_row++; // increment current row index
// grab info for the next class
$row = mysql_fetch_row($result);
$name = $row[0];
$description = $row[1];
$location = $row[2];
$url = $row[3];
$day = $row[4];
$start_time = $row[5];
$end_time = $row[6];
// compute latest end time
$latest_end_time= $end_time > $latest_end_time ? $end_time : $latest_end_time;
} else {
$done = true;
}
} else {
// there is no class, allow user to add a class if a cell is not supposed to be skipped
if ($skipcell > 0) {
$skipcell--;
} else {
echo "<td class=\"empty-cell\" onMouseOver=\"showAddClass(this)\"></td>";
}
}
}
echo "</tr>";
} // end print new timetable
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment