gpt4 book ai didi

php - 将用户的学校时间表打印为表格的算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:34 25 4
gpt4 key购买 nike

我正在尝试在表格中绘制学生类(class),其中表格单元格可以包含类(class),也可以为空。表格行以一天中的时间(以分钟为单位)开头,列代表星期几(星期日 = 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>&nbsp;</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> &nbsp; </td>";
$styles = "class='hiddenTopRow'";
$printed = TRUE;
} else if ( $i > $latest ) {
$rowContent .= "<td> &nbsp; </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> &nbsp; </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>

最佳答案

要查看发生了什么,只需在第 84 行打印索引 $j:

    if ( $i + 30 < $earliest ) {
$rowContent .= "<td> $j </td>";
$styles = "class='hiddenTopRow'";
$printed = TRUE;
}

问题是这样的:当您为单元格/ block 设置行跨度时,在接下来的行中您不知道应该跳过哪个单元格/ block 。

您可以使用数组来跟踪此信息 (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> &nbsp; </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
[...]

关于php - 将用户的学校时间表打印为表格的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18386284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com