我正在尝试在表格中绘制学生类,其中表格单元格可以包含类,或者为空。表行以时间(以分钟为单位)为标题,列表示星期几(星期日= 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>
答案 0 :(得分:0)
要查看发生了什么,只需在第84行打印索引$ j:
if ( $i + 30 < $earliest ) {
$rowContent .= "<td> $j </td>";
$styles = "class='hiddenTopRow'";
$printed = TRUE;
}
问题在于:当您为单元格/块设置rowspan时,在以下行中您不知道应跳过哪个单元格/块。
您可以使用数组来跟踪此信息(http://phpfiddle.org/main/code/03w-0v6)。 例如,在下面的代码中我添加了变量$ rowJump。 如果我设置$ rowJump [2] = 2,则意味着我必须在接下来的两行中跳过单元格2。
$rowJump = Array();
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
if (isset($rowJump[$j]) && $rowJump[$j]>0) {
$rowJump[$j]--;
continue;
}
$printed = FALSE;
if ( $i + 30 < $earliest ) {
$rowContent .= "<td> $j </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;
$rowJump[$j] = $rowspan-1;
$j++;
} else
[...]