duanlan5320 2012-04-24 05:26
浏览 27
已采纳

在PHP中如何处理填充时间的HTML选择框并排除MySQL中已有的那些

I am trying to populate a table with times the client has booked an appointment(eg. 10:30AM) so I have a select box with the times of the day starting from 9:00AM to 7:00PM going up in 15minute steps(9:45am, 10:00am).

<select name="times">
<option value="09:00">9:00am</option>
.....
<option value="19:00">7:00pm</option>
</select>

I need to some how delete some of the times that are already stored in the database

would this be like?:

<?php
$times = $_POST['times'];
$dbc = mysqli_connect('localhost', 'root', '', 'salon')
or die('error conneting to mysql');

$query = "SELECT times FROM `clients` WHERE `times` = '$times'";

$result = mysqli_query($dbc, $query);
mysqli_close($dbc);
?>
<select>
<?php
$times = array('09:00','09:15', ..... '19:00');
foreach($times as $time);
while($row = mysql_fetch_array($results)){
 if($row = $time){
unset($time);

echo '<option value="$time">$times</option>';
}
else{ echo 'oh no';
}
?>
</select>

well something like that.. anyway I just can't figure it out.

thanks in advance to all help.

P.S I know my code should be more clean and include strip_tags() mysql_real_escape_string(). this is just for the purpose of asking a question

  • 写回答

1条回答 默认 最新

  • duanposhi3641 2012-04-24 08:51
    关注

    This is totally premature example of a code. There are a lot of errors in your code. BTW following is the correct one :

    $times = $_POST['times'];

    is an array so you can't use it like a string. You have to implode the array using comma (,) separator so that your variable $times could be like this

    $times_array = "in('09:00','09:30')";

    Following is full example

    <?php
    
        $times_post = $_POST['times'];
    
        $times_array = "in('09:00','09:30')";// This will be your string after imploding post array
    
        $dbc = mysqli_connect('localhost', 'root', '', 'shiv')  or die('error conneting to mysql');
    
        $query = "SELECT times FROM `clients` WHERE `times` = $times_array";
    
    
        $results = mysqli_query($dbc, $query);
    
        echo "<select>";
    
        $times = array('09:00','09:15','19:00');
    
        while($row = mysqli_fetch_array($results)){
    
            foreach($times as $key => $time){
    
                if($row[0] == $time){
                    unset($times[$key]);
    
                }else{
                    echo 'oh no';
                }
            } 
        }
    
        $updated_times = array_values($times);
    
        for ($index = 0; $index < count($updated_times); $index++){
            echo "<option value=$updated_times[$index]>$updated_times[$index]</option>";
        } 
    
        echo "</select>";
    
    ?>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法