循环只检查数组的第一部分

时间:2013-02-24 15:40:20

标签: php mysql calendar

我正在制作用户进行预订的日历系统(在不同的页面上)。在此页面上,用户单击相关框以选择新预订的开始时间。我正在检查数据库中是否有现有的预订,并使那些无法点击以防止双重预订。

我的问题是,为什么当天只有第一张预订出现在桌面上?

变量$booked是否仍为真,因此不显示其他预订?我无法理解它!

MySQL的:

$query="SELECT * FROM bookings WHERE DateBooked = '{$year}-{$month}-{$selectedday}' AND Approved = 1";
$result = mysql_query($query);
$todayarray = mysql_fetch_assoc($result);

和PHP:

while ($room <= $roomcount) {
    echo "\n<div class=\"roomtimes\">";
    echo "\n<table border=1>";
    echo "\n<tr><th class=\"titlecell\">Room $room</th></tr>";
    $cellnum = 10;
    while ($cellnum <= 22) {
        if (($todayarray['StartTime'] <= $cellnum) && ($todayarray['EndTime'] >= $cellnum) && ($todayarray['Room'] == $room)) {
            $booked = true;
        } else {
            $booked = false;
        }
        echo "\n<tr>";
        if ($booked) {
            echo "\n<td class=\"blankcell";
        } else {
            echo "\n<td class=\"linkcell";
        }

        if ($selectedtime == $cellnum) {
            echo " selectedcell";
        }
        echo "\">";
        if ($booked) {
            echo "$cellnum:00 --BOOKED--";
        } else {
            echo "<a href=\"newbooking.php?m=$selectedmonth&d=$selectedday&t=$cellnum&r=$room\">$cellnum:00</a>";
        }
        echo "</td>";
        echo "\n</tr>";
        $cellnum++;
    }
    $room++;
    echo "\n</table>";
    echo "\n</div>";
}
echo "\n</div>";
?>

1 个答案:

答案 0 :(得分:0)

问题是您只是从表中查看结果集的第一行。您只需调用mysql_fetch_assoc($result)一次,此函数一次只返回一个结果行。您需要遍历从查询调用中获得的$result。您可以使用类似于此的while循环:

while ($row = mysql_fetch_assoc($result) {
   //this can be your php code from above 
}