2

我有一个 for 循环通过单元格输出一天 24 小时<td>。我想从数据库中检索我的开始时间和结束时间,并将数据库colspan中的记录显示到表中。我可以使用从数据库返回的行数成功删除额外的<td>单元格,但我知道有一种更好的方法可以输出<td>单元格,colspan而无需通过行数重复。 http://i.imgur.com/ZMbKILm.jpg

// Interval in seconds
$interval = 1800;

$date_first     = "02-09-2013 00:00:00";
$date_second    = "03-09-2013 00:00:00";

$time_first     = strtotime($date_first);
$time_second    = strtotime($date_second);

for ($i = $time_first; $i <= $time_second; $i += $interval) {
    $timeOut = date('d-m-Y H:i', strtotime("+30 minutes", $i));

    $a = $data->query("SELECT * FROM table WHERE $timeOut BETWEEN starttime AND endtime AND item = '1' AND active = 'true'");

    if ($a->num_rows > 0) {
        $b = $a->fetch_array();
        $starttime = $b["starttime"];
        $endtime = $b["endtime"];
        $colspan = $endtime - $starttime;
        // The items with colspan should be here
        // but they output too many times.
        // I need some way to exit this after each output.
        echo '<td colspan="'.$colspan.'"></td>';
    } else {
        echo '<td></td>';
    }
}
4

1 回答 1

1

正确设置下一次迭代:

if ($a->num_rows > 0) {
    $b = $a->fetch_array();
    $starttime = $b["starttime"];
    $endtime = $b["endtime"];
    $colspan = $endtime - $starttime;
    // The items with colspan should be here
    // but they output too many times.
    // I need some way to exit this after each output.
    echo '<td colspan="'.$colspan.'"></td>';
    $i = $time_first + $interval*ceil(($endtime-$time_first)/$interval);
    // or, $i += $interval*$colspan;
} else {
    echo '<td></td>';
}
于 2013-09-11T22:04:15.327 回答