0

我正在制作每支球队参加的运动(羽毛球)的搜索页面:早上,下午,晚上。一个星期可以有三个游戏时间。所以表格是这样的:

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')";
}

有两个问题:

  1. 有没有更好的方法来编写更少的代码以使其运行得更快?
  2. 如何避免检查 start_time 是否为 00:00:00?因为如果我不跳过检查,如果我只想早上搜索,则条件为真。因为早晨的时间小于 12:00:00,而 00:00:00 小于 12:00:00。
4

1 回答 1

0

你为什么要经过if ($value == "早上") $morning = "checked";?只需使用 value 来更改查询。

 if ($value == "早上"){
        $criteria .= "and (start_time < '12:00:00' or start_time2 < '12:00:00' or start_time3 < '12:00:00')";
    }
  elseif ($value == "下午"){
       $criteria .= "and (start_time < '18:00:00' or start_time2 < '18:00:00' or start_time3 < '18:00:00')";  
    }
    elseif ($value == "value3"){
       $criteria .= "query3";  
    }

如果您愿意,您可以将 'if' 替换为 'switch' 大小写

switch ($value) {
    case 0:
        $criteria .= "query1" 
        break;
    case 1:
         $criteria .= "query2" 
        break;
    case 2:
         $criteria .= "query3" 
        break;
}
于 2013-01-06T17:21:49.347 回答