我正在尝试在表格中绘制学生班级,其中表格单元格可以包含班级,也可以为空。表格行以一天中的时间(以分钟为单位)为标题,列表示一周中的哪一天(星期日 = 1、星期三 = 4、星期六 = 7 等)。
当三个类同时出现时,我的算法似乎存在问题,因为打印了太多的表格单元格。我认为问题与$printed
成为一个boolean
(它可能应该是从 7 开始的倒数)有关,但我不确定。
有人可以阐明我哪里出错了吗?这是我的代码: http: //phpfiddle.org/main/code/q6z-s93。点击F9
示例输出。
<?php
/**
* Created by JetBrains PhpStorm.
* User: Marco
* Date: 8/22/13
* Time: 9:49 AM
*/
$name = "Table Name";
$blocks = array(
array(
"id" => 23,
"name" => "test",
"day" => 2,
"startTime" => (9 * 60) + 30,
"endTime" => 720
),
array(
"id" => 12,
"name" => "test 2",
"day" => 3,
"startTime" => (10 * 60) + 30,
"endTime" => 720
),
array(
"id" => 2,
"name" => "test 2",
"day" => 4,
"startTime" => (9 * 60) + 30,
"endTime" => (10 * 60) + 30
)
);
$tableContent = "";
$tableContent.= "
<h1 id='1' class='table-name'>$name</h1>
<table class='table table-bordered' data-id='1' border='1'>
<thead>
<tr>
<th> </th>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
</thead>
<tbody>
";
$time = mktime(0, 0, 0, 1, 1);
$earliest = 720;
$latest = 1140;
for( $i = 0; $i < 1440; $i += 30 ) {
// print table rows
$rowContent = ""; // Holds table cells and content
$styles = ""; // holds `class="foo"` (row class)
for ($j = 1; $j < 8; $j++) {
// print row columns
$printed = FALSE;
if ( $i + 30 < $earliest ) {
$rowContent .= "<td> </td>";
$styles = "class='hiddenTopRow'";
$printed = TRUE;
} else if ( $i > $latest ) {
$rowContent .= "<td> </td>";
$styles = "class='hiddenBottomRow'";
$printed = TRUE;
}
foreach ( $blocks as $block ) {
// cycle through Courses and check if there is one scheduled at this time
if ( ( $block["day"] == $j ) && ( $block["startTime"] == $i ) ) {
// class starts on this day at this time
$rowspan = ( ( $block["endTime"] - $block["startTime"] ) / 30 );
$content = $block["name"];
$blockID = $block["id"];
$rowContent .= "\t" . "<td rowspan='$rowspan'
data-id='$blockID'
class='block-cell'>$content</td>" . "\r\n";
$printed = TRUE;
} else if ( ( $block["day"] == $j ) && // Class starts this day
( $block["startTime"] < $i ) && // after this time
( $block["endTime"] >= $i + 30) ) { // but isn't finished
// class is continuing
$printed = TRUE;
} else {
// no class at this time
}
}
if (!$printed) $rowContent .= "<td> </td>";
}
/* Print content */
$tableContent .= "<tr $styles>" . "\r\n";
$heading = sprintf("\t" . '<th class="time">%1$s</th>' . "\r\n",
date( 'g:i a', $time + ( $i * 60 ) ) );
$tableContent .= $heading . $rowContent;
$tableContent .= "\t" . "</tr>" . "\r\n";
}
$tableContent .= '
</tbody>
</table>
';
?>
<!DOCTYPE html>
<html lang="en">
<body>
<?php echo $tableContent ?>
</body>
</html>