【问题标题】:PHP For Loop, Matching Outputs With Database ResultsPHP For 循环,将输出与数据库结果匹配
【发布时间】:2017-04-29 03:52:14
【问题描述】:

我觉得我在这里遗漏了一些简单的东西,并且在构建“安排约会”日历时有点卡住了。

客户可以预订他们自己的时间段,当他们这样做时......我需要让其他人无法使用这些时间段。

我在上午 9 点到晚上 7 点之间每隔 30 分钟使用“for 循环”下拉菜单来选择“开始时间”。如果数据库中保存了“10:00am”的“开始时间”和“11:30am”的“结束时间”...10:00am、10:30am和11:00am需要从表单提交成功后的“开始时间”下拉菜单。

(数据库结果以字符串的形式...仅供参考)

为此,我有这个:

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

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

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

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

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

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

            $the_time = date("h:ia", $y) . "<br>";

            $starting = array();

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

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

                }

            if(in_array($the_time, $starting)){
            echo "<option>Not Available</option>";
            } else {
            echo "<option>" . $the_time . "</option>";
            }

}
        echo'</select>';

任何建议,完成这个或比“in_array”更好的方法来完成我想要做的事情?

提前谢谢你。

【问题讨论】:

  • 为什么while 部分在for 部分中?在for 的第一次迭代之后,mysqli_fetch_assoc 将不提供任何信息。
  • 好电话。我将while循环移到for循环之前,这解决了一个问题。另一个问题是 - 在进行比较时,我没有考虑附加到 $the_time 字符串的“
    ”。感谢您的帮助。

标签: php arrays for-loop mysqli


【解决方案1】:

解决方案是将“while 循环”移到“for 循环”之外,我没有考虑附加到 $the_time 变量末尾的“br”。

这是工作代码:

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

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

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

    $starting = array();

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

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

                }

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

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

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

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

            if(in_array($the_time, $starting)){
            echo "<option>Not Available<br></option>";
            } else {
            echo "<option>" . $the_time . "<br></option>";
            }
        } 

【讨论】:

  • "br" 可以附加到 "echo".echo "";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多