【发布时间】:2014-01-28 13:37:58
【问题描述】:
更新
我编辑了我的代码,所以现在时间检查正确:
while($row = mysqli_fetch_array($result)){
$rid = $row['roomid'];
$begin = $row['start'];
$bval = strtotime($begin);
$einde = $row['end'];
$eval = strtotime($einde);
$staco = strtotime($start); //starttime posted from form
$endco = strtotime($end); //stoptime posted from form
$abegin = array(sta => $bval);
$aeinde = array(sto => $eval);
// print_r($abegin);
// print_r($aeinde);
foreach($aeinde as $sto) {
if($staco <= $sto) {$checksto = 1;}
else {$checksto = 0;}
}
foreach($abegin as $sta) {
if($endco <= $sta) {$checksta = 1;}
else {$checksta = 0;}
}
if($checksta == $checksto) {$ok = 1;} else {$ok = 0;}
print_r($ok);
}
}
}
那么现在:我如何检查 $ok 是否包含一个或多个 0(不预订房间)或所有 1(预订房间)。
$roomstate = array(state => $ok) 产生多个数组:
Array ( [state] => 0 ) Array ( [state] => 1 ) Array ( [state] => 1 )
我做错了什么,因为我认为应该可以将所有不同的$ok 放在一个数组中,然后
if(in_array(0,$roomstate)) { echo "Do not book";} else {$bookitsql = "INSERT INTO reservations ...";}
更新:我的原始逻辑中存在一个缺陷,即检查需要首先解决的房间的可用性:现在房间没有检查正确,因此无法回答这个问题,因为没有显示正确的数据。我很抱歉。
对于预订房间的系统,如果房间目前已经被预订,我需要检查新的预订。预订使用表格,然后将表格中的结果与该房间在该日期的数据库中的内容进行比较。
而($row = mysqli_fetch_array($result)){
$rid = $row['roomid'];
$begin = $row['start'];
$bval = strtotime($begin);
$staco = strtotime($start);
$einde = $row['end'];
$eval = strtotime($einde);
$endco = strtotime($end);
$abegin = 数组(sta => $bval);
$aeinde = 数组(sto => $eval);
foreach($abegin as $sta) {
if($staco $checksta,checkstop => $checksto);
print_r($meh);
}
更好的问题:
现在我得到了 `$staco` 和 `$endco`,它们是表单的开始和停止时间。
我还得到了 `$sta` 和 `$sto`,它们是来自数据库的多个开始和停止时间。
例子:
现有保留:
斯塔斯托
1:0800 0959
2:1130 1259
所以现在当我得到 `$staco = 1000` 和 `$endco = 1114` 时,它检查不正确。
它仅在新预订晚于数据库中的所有其他预订时才有效。我该如何解决这个问题?
【问题讨论】:
-
旁注: 不再支持
mysql_*函数,它们是officially deprecated,不再维护,将来会是removed。您应该使用PDO 或MySQLi 更新您的代码,以确保您的项目在未来的功能。 -
一次检查所有值。现在它检查数据库中每一行(在那个日期和房间)的 $checksta 和 $checksto。所以现在我有时会得到结果
Array ( [checkstart] => 1 [checkstop] => 0 ) Array ( [checkstart] => 0 [checkstop] => 0 ),所以它不能添加预订。但是由于第二个数组,它会认为无论如何都可以预订房间 -
@AmalMurali 我的 sql 语句现在可以与 mysqli 一起使用。