回显SQL结果以创建时间表

时间:2015-10-05 20:06:53

标签: php html sql echo

我在使用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结果。知道我怎么能让这个工作。

1 个答案:

答案 0 :(得分:0)

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

//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