我正在制作每支球队参加的运动(羽毛球)的搜索页面:早上,下午,晚上。一个星期可以有三个游戏时间。所以表格是这样的:
start_time = "08:00:00"
start_time2 = "12:00:00"
start_time 3 = "18:00:00"
或者如果球队一周只打一场比赛,表格是这样的:
start_time = "08:00:00"
start_time2 = "00:00:00"
start_time3 = "00:00:00"
现在我的查询是这样的:
if (isset($_REQUEST['time'])){
foreach ($time as $value){ //make day stay checked 2012/12/31
if ($value == "早上") $morning = "checked";
if ($value == "下午") $afternoon = "checked";
if ($value == "晚上") $evening = "checked";
}
if ($morning == "checked" and $afternoon != "checked" and $evening != "checked"){ //only morning
$criteria .= "and (start_time < '12:00:00' or start_time2 < '12:00:00' or start_time3 < '12:00:00')";
}elseif ($morning == "checked" and $afternoon == "checked" and $evening != "checked"){ //morning + afternoon
$criteria .= "and (start_time < '18:00:00' or start_time2 < '18:00:00' or start_time3 < '18:00:00')";
}elseif ($morning == "checked" and $afternoon != "checked" and $evening == "checked"){ //morning + evening
$criteria .= "and ((start_time < '12:00:00' and start_time >= '18:00:00') or (start_tim2e < '12:00:00' and start_time2 >= '18:00:00') or (start_time3 < '12:00:00' and start_time3 >= '18:00:00'))";
}elseif ($morning != "checked" and $afternoon == "checked" and $evening != "checked"){ //afternoon
$criteria .= "and ((start_time >= '12:00:00' and start_time < '18:00:00') or (start_time2 >= '12:00:00' and start_time2 < '18:00:00') or (start_time3 >= '12:00:00' and start_time3 < '18:00:00'))";
}elseif ($morning != "checked" and $afternoon == "checked" and $evening == "checked"){ //afternoon + evening
$criteria .= "and (start_time >= '12:00:00' or start_time2 >= '12:00:00' or start_time3 >= '12:00:00')";
}elseif ($morning != "checked" and $afternoon != "checked" and $evening == "checked"){ //only evening
$criteria .= "and (start_time >= '18:00:00' or start_time2 >= '18:00:00' or start_time3 >= '18:00:00')";
}
有两个问题:
- 有没有更好的方法来编写更少的代码以使其运行得更快?
- 如何避免检查 start_time 是否为 00:00:00?因为如果我不跳过检查,如果我只想早上搜索,则条件为真。因为早晨的时间小于 12:00:00,而 00:00:00 小于 12:00:00。