PHP For循环,将递增值与递增数组进行比较

时间:2016-12-14 14:53:50

标签: php mysql arrays if-statement for-loop

  • 简短的问题:我正在尝试查看变量($y) 之间的来自数据库($ending[0] && $starting[0], $ending[1] && $starting[1]等)的多个数组值。 " 如果" $y介于其中echo "statement"之间。{/ 1}

我使用for循环以 30分钟间隔从上午9点 - 晚上7点递增下拉。

如果这些时间中的任何一个=我的数据库中保存的时间,它回应" 不可用"代替。

现在我需要看到" 如果"下拉时间为" "数据库中的开始和结束时间,并使那些"不可用"同样。

如果我手动识别要比较的数组键,那么一切正常......但我的问题是:我可以针对数组中的所有值检查下拉值吗?

实施例: 开始下拉:09:00 am 09:30 am上午10:00等,直至07:30。结束下拉:上午09:30上午10:00上午10:30等,直到晚上08:00。如果上午9:00(开始)上午10:00(结束)的开始和结束时间保存在数据库中进行预约,则下拉列表将显示"不可用"对于相应的开始和结束时间,因此用户不能双重预约。我要问的是,如何识别"之间的所有"价值...例如09:30 am,如果已经预订了09:00 am-10:00am的话,则不能是开始时间或结束时间。

$sql_slots = "SELECT * FROM XXXXX WHERE date = '$query_date' ORDER BY XXXXX ASC";   

    $result_slots = mysqli_query($connection, $sql_slots);  

    $open = strtotime("9:00am");

    $close = strtotime("9:30am");

    $starting = array();
    $ending = array();

            while($row_result_slots = mysqli_fetch_assoc($result_slots)){   

                $starting[] = $row_result_slots['start_time'];

                $ending[] = $row_result_slots['end_time'];      

                }

    echo'
    <select name="start_time">';

        for($b = 0; $b <= 20; $b++){

            $y = strtotime(($b*30) . " Minutes", $open);

            $the_time = date("h:ia", $y);

            // Here, instead of only comparing the first values in the array, I need to match it up against $ending[1] & $starting[1], $ending[2] & $starting[2], etc... 

if(in_array($the_time, $starting) || ($y <= strtotime($ending[0]) && $y >= strtotime($starting[0]))){
                echo "<option>Not Available<br></option>";
                } else {
                echo "<option>" . $the_time . "<br></option>";
                }
            } 

        echo'</select>';

2 个答案:

答案 0 :(得分:0)

那怎么样?

$sql_slots = "SELECT * FROM XXXXX WHERE date = '$query_date' ORDER BY XXXXX ASC";   

$result_slots = mysqli_query($connection, $sql_slots);  

$open = strtotime("9:00am");

$close = strtotime("9:30am");

$reserved = array();

        while($row_result_slots = mysqli_fetch_assoc($result_slots)){   

            $start = $row_result_slots['start_time'];

            while ($start <= $row_result_slots['end_time']) {

                $reserved[$start] = true;

                $start = date("Y/m/d h:i:s", strtotime($start . "+30 minutes"));
            }
        }

echo '<select name="start_time">';

    for($b = 0; $b <= 20; $b++){

        $y = strtotime(($b*30) . " Minutes", $open);

        $the_time = date("h:ia", $y);

        if(isset($reserved[$the_time]){
            echo "<option>Not Available<br></option>";
            } else {
            echo "<option>" . $the_time . "<br></option>";
            }
        } 

    echo'</select>';

答案 1 :(得分:0)

以下是我的想法。这使用do while循环并创建一个reserved数组,该数组将填充约会中给出的所有时间。

我更新了数据以使用我自己的数组,但更新了mysqli工作的正确性。如果没有,我留在我的测试代码中供您查看是否需要。我还使用disabled attribute上的option <?php $sql_slots = "SELECT * FROM XXXXX WHERE date = '$query_date' ORDER BY XXXXX ASC"; $result_slots = mysqli_query($connection, $sql_slots); $open = strtotime("9:00am"); $close = strtotime("7:00pm"); $space = 900; //15min * 60 seconds space between? //This seems more normal for people to see. //Simulate these appoinments /* $query_results = array( array( "start_time" => "9:00am", "end_time" => "9:30am" ), array( "start_time" => "10:30am", "end_time" => "11:30am" ), array( "start_time" => "11:45am", "end_time" => "12:30pm" ) ); */ $reserved = array(); while($row_result_slots = mysqli_fetch_assoc($result_slots)){ //**NOTE**: Make sure these are times or use strtotime() $next_slot = $reserved[] = strtotime($row_result_slots['start_time']); $end_time = strtotime($row_result_slots['end_time']); while($next_slot < $end_time){ $next_slot += $space; $reserved[] = $next_slot; } } /* foreach($query_results as $appoinment) { $next_slot = $reserved[] = strtotime($appoinment['start_time']); $end_time = strtotime($appoinment['end_time']); while($next_slot < $end_time){ $next_slot += $space; $reserved[] = $next_slot; } } */ echo'<select name="start_time">'; $current = $open; do { $disabled = in_array($current, $reserved) ? 'disabled' : ''; echo '<option ' . $disabled . '>' . date("h:ia", $current) . '</option>'; $current += $space; } while($current <= $close); echo'</select>'; 输出了一些文字。

app.get('/profile', function(req, res){
var msg = req.flash('message');
var username1 = req.user.username;
Products.find({'username': username1}).sort('-date').exec(function(err, docs){
    res.render('profile', { title: 'Products', products: docs, flashmsg: msg});
});
});