我制作了一个会议室预约系统,将会议室的预约情况用表格展示了出来,我想制作一个功能,点击表格中的空位即可预约这个空位所指的日期和时间。
我使用了form表单来传递数据,将表单夹在表格中,使用for循环输出表格的时候顺便输出多个表单时出现问题,所有表单传送的数据都是最后一个表格的
代码如下:
for($time=9;$time<18;$time++){
echo "<tr>";
echo "<td>";
if($time == 12){//跳过午休时段
$time = 14;
}
if($time == 9){//将9改为09
$timea = "0".$time;
}else{
$timea = $time;
}
$timeb = $time +1;
$time1 = $timea.":00:00";
$time2 = $timeb.":00:00";
//为了显示好看进行的格式修正
$timeshow1 = $timea.":00";
$timeshow2 = $timeb.":00";
echo $timeshow1."-".$timeshow2;
echo "</td>";
for($i=0;$i<5;$i++){//循环输出5天的预约情况
echo "<td>";
$datemark = 0;//日期匹配符
$timemark = 0;//时间匹配符号
//用(datemark,timemark)来判定,(0,x)和(1,0)都代表匹配失败,(1,1)匹配成功
$date = date("Y-m-d",strtotime("+$i day"));
foreach($rows as $row): //循环所有预约,进行3次判断,第一次判定日期,第二次判定开始时间,第三次判定结束时间
if($row['date'] == $date){
$datemark = 1;//日期匹配成功,日期匹配符置1
if($row['begintime'] < $time2){
if($row['finishtime'] > $time1){//这两次是在判断时间段是重叠/
if($row['mark'] == "通过"){//全部匹配成功并且预约以及通过
echo "已被占用";
$timemark = 1;
break;
}else if($row['mark'] == "待审批"){//全部匹配成功但是预约还没有被通过
?>
<form action="roomuse-create.php" method="POST">
<input type="hidden" name="rid" value=<?= $row['rid'] ?>>
<input type="hidden" name="rname" value=<?= $row['rname'] ?>>
<input type="text" name="date" value=<?= $date?>>
<input type="text" name="begintime" value=<?= $time ?>>
<input type="hidden" name="mark" value='1'>
<input type="submit" value="已被预约">
</from>
<?php
$timemark = 1;
break;
}
}
}
}
endforeach;
if($datemark == 0){//所有预约的日期都匹配失败,今天肯定空闲
?>
<form action="roomuse-create.php" method="POST">
<input type="hidden" name="rid" value=<?= $row['rid'] ?>>
<input type="hidden" name="rname" value=<?= $row['rname'] ?>>
<input type="text" name="date" value=<?= $date?>>
<input type="text" name="begintime" value=<?= $time ?>>
<input type="hidden" name="mark" value='1'>
<input type="submit" value="可预约1">
</from>
<?php
}
if($datemark == 1 and $timemark == 0){//日期匹配成功,时间错误,这个格子仍然输出可预约
?>
<form action="roomuse-create.php" method="POST">
<input type="hidden" name="rid" value=<?= $row['rid'] ?>>
<input type="hidden" name="rname" value=<?= $row['rname'] ?>>
<input type="text" name="date" value=<?= $date?>>
<input type="text" name="begintime" value=<?= $time ?>>
<input type="hidden" name="mark" value='1'>
<input type="submit" value="可预约2">
</from>
<?php
}
echo "</td>";
}
echo "</tr>";
}