【问题标题】:Echo out SQL result to create a timetable回显 SQL 结果以创建时间表
【发布时间】:2023-03-15 20:24:01
【问题描述】:

我在使用 SQL 语句的结果和 PHP 的一些 HTML 布局打印时间表时遇到问题。

我在页面顶部有上课时间。 我试图将星期几放在页面的一侧,然后检查是否应在特定日期和时间打印 SQL 语句(包含模块)的结果。

//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th></th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";

//Now loop through the result of the SQL statement that contains the modules associated with the selected course
while($row = mysqli_fetch_array($result1)) {
    for($d = 1; $d < 6; $d++){
        $printday = $days[$d];
        echo "$printday";

        for($t = 9; $t < 17; $t++) {
            if($row['Day'] == $d && $row['Time'] == $t){ //fix this area so that it moves along
                echo "<td>" . $row['ModuleName'] . "<br/>\n " .$row['Location'] . "</td>";
            } //if
            else {
                echo "<td></td>";
            } //else
        } //for 2

        echo "</tr>";
    } //for 1
} //while

问题是我在周一至周五打印了 3 次,因为有 3 个 $row 结果。任何想法我怎么能让这个工作。

【问题讨论】:

  • echo "&lt;td&gt;&lt;/td&gt;"; 应该是 echo "&lt;td&gt;&amp;nbsp;&lt;/td&gt;"; - 至于其余的,我不明白你想要做什么。
  • 问题不是他的 HTML.. 只需将其删除为块并使用其中的内容。
  • @Pamblam 我敢打赌这些数字代表 9-5 个工作日
  • @Pamblam 顶部 9-5 个工作日。周一至周五下来。由于我有 3 个结果,它会将第一行发布在正确的位置,但随后会打印出其余的日子。然后它转到第二个结果并执行此操作。因此,而不是 5 天显示 3 个模块。我在周一至周五展示了 3 次(共 15 次)和 3 个模块

标签: php html sql echo


【解决方案1】:

您在错误的位置循环数据,您首先需要将其放入一个数组中,以便您可以在每一天/时间循环它。

//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th>Day</th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";

//Now loop through the result of the SQL statement that contains the modules associated with the selected course
$courses = array();
while($row = mysqli_fetch_array($result1)) {
    $courses[] = $row;
} //while

for($d = 1; $d < 6; $d++){
    $printday = $days[$d];
    echo "<tr><td>" . $printday . "</td>";

    for($t = 9; $t < 17; $t++) {

        unset($course_row);

        foreach($courses as $course){
            if($course['Day'] == $d && $course['Time'] == $t){ //fix this area so that it moves along
                $course_row .= $course['ModuleName'] . "<br/>\n " .$course['Location'] . "<br/>\n<br/>\n";
            } //if
        }

        if(isset($course_row)){
            echo "<td>" . $course_row . "</td>";
        } else {
            echo "<td>&nbsp;</td>";
        } //else
    } //for 2

    echo "</tr>";
} //for 1

【讨论】:

  • 感谢您的回答,它几乎完全按照我的意愿工作。我有一个我不太明白的错误:.....注意:未定义的变量:C:\xampp\htdocs\judeProject\502\generateTT.php 中的 course_row 在第 70 行....你知道问题出在哪里?谢谢
  • 它是因为我在这里使用.= $course_row .= $course['ModuleName'],并且我正在这样做,所以您的日程安排将显示该时间安排的尽可能多的课程......如果安排了 4 个课程怎么办?那时。如果这不会发生,只需将其更改为 =
  • 您的回答很完美,但就我而言,没有同时安排课程。我会牢记这一点,以备将来之用。感谢您的帮助,非常感谢
  • @JudeHargan 请接受正确的答案!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多