使用while禁用选项

时间:2015-09-12 03:38:29

标签: php mysqli

如果数据库中的值相等,我有此代码用于禁用该选项。在我的数据库中,我有凌晨1:00,凌晨1:30,凌晨2:00,凌晨2:30。但它只禁用凌晨2:30我应该怎么做我尝试这个代码,但什么都没发生?

     if(isset($_POST["doc"]))
$_SESSION["doc"] = $_GET["doc"];        
                  $dr=$_SESSION['doc'];
   $q = mysqli_query($link,"select * from doctor where DoctorName='". $dr ."'");
   $r =mysqli_fetch_Array($q);
$tf = $r["TimeFrom"];
         $tt = $r["TimeTo"];   


   ?>
     <?php
      if(isset($_POST["iDate"])){
           $st = "Pending";
    $app = "Approved";

       $qq2= mysqli_query($link,"select * from appointment where  Status='". $app ."' and PrefferedDate='". $date ."' and DoctorId='". $w."'");
     while($rrr = mysqli_fetch_array($qq2)){

    echo     $time=$rrr["PrefferedTime"];
     }
   ?>

  <tr><td>Time:<td><select name="time" class="form-control" id="time" required>
<option>  </option>
<?php

if($tf==1 && $tf<=$tt){
?>
<option value="1"
<?php
 if($time== "1:00 am "   ){
     echo "disabled";} else{
     echo "";}?>>1:00 am</option>
<?php
   $tf++;
 }
   ?>
<?php
if($tf==2 && $tf<=$tt){
?>
<option value="2"
<?php
 if($time== "1:30 am "  ){
     echo "disabled";} else{
     echo "";}?>>1:30 am</option>
<?php
   $tf++;
 }
  ?>
<?php
if($tf==3 && $tf<=$tt){
?>
<option value="3"
<?php
 if($time== "2:00 am "    ){
     echo "disabled";} else{
     echo "";}?>>2:00 am</option>
<?php
   $tf++;
 }
 ?>
<?php
if($tf==4 && $tf<=$tt){
?>
<option value="4"
<?php
 if($time== "2:30 am "){
     echo "disabled";} else{
     echo "";}?>>2:30 am</option>
<?php
   $tf++;
 }

?>
<?php
if($tf==5 && $tf<=$tt){
?>
<option value="5"
<?php
 if($time== "3:00 am " ){
     echo "disabled";} else{
     echo "";}?>>3:00 am</option>
<?php
   $tf++;
 }
 ?>

1 个答案:

答案 0 :(得分:1)

这是你要找的吗?

<?php

if (isset($_POST["iDate"])) {

    $st = "Pending";
    $app = "Approved";

    $taken = [];
    $query = mysqli_query($link, "select PrefferedTime from appointment where  Status='{$app}' and PrefferedDate='{$date}' and DoctorId='{$w}'");
    while ($row = mysqli_fetch_array($query)) {
        $taken[] = $row["PrefferedTime"]; // collect all present values
    }

    function is_taken($appointment, $all) {
        return count(array_filter($all, function ($v) use ($appointment) {
            return trim($v) == trim($appointment);
        })) > 0;
    }

    $appointments = [];
    for ($i = 1; $i < 24 * 2 - 1; $i++)
        $appointments[$i] = date('g:i a', $start + ($i - 1) * (60 * 30));

    $options = [];
    foreach ($appointments as $id => $label) { // loop through all available
        if (($id >= $tf) && ($id <= $tt))
            $options[] = '<option value="' . $id . '" ' 
                         . (is_taken($label, $taken) ? 'disabled' : '') // disable if present in db
                         . '>' . $label . '</option>';
    }

?>

    <tr><td>Time:<td><select name="time" class="form-control" id="time" required>
        <option>  </option>
        <?php echo join(PHP_EOL . "\t", $options); //output all options, taken - grayed ?>
    </select>

<?php

}

?>