disable option in drop-down list

0

I have an error in the code I am doing, since it is duplicating the options of a list (agenda) that lasts for 15 minutes.

The form records everything very well and when I come with the foreach to disable the drop-off list the time that has already been previously taken to avoid duplicating appointments disables me very well, the problem is that if there are for example 20 cited , the list repeats it to me 20 times ... how do I make it so that it is not repeating the list and only shows me the previously selected time disabled?

Here's an image:

Here the current code:

            <select name="escogerHora">
            <?php
                $id = '1';
                $fechaEscogida = '2018-08-28';
                $desde = '14:30:00';
                $hasta = '19:00:00';
                $sth = $conn->prepare("SELECT TIME(fecha) AS fecha
                FROM AGENDA WHERE id = '".$id."' AND DATE(fecha) = '".$fechaEscogida."'
                AND TIME(fecha) BETWEEN '".$desde."' AND '".$hasta."'
                ");
                $sth->execute();
                $results = $sth->fetchAll();
                foreach($results as $row) :
            ?>
                <option value="14:30" <?php if ($row["fecha"] == '14:30:00') {echo 'disabled="disabled"';} ?>>2:30 P.M.</option>
                <option value="14:45" <?php if ($row["fecha"] == '14:45:00') {echo 'disabled="disabled"';} ?>>2:45 P.M.</option>
                <option value="15:00" <?php if ($row["fecha"] == '15:00:00') {echo 'disabled="disabled"';} ?>>3:00 P.M.</option>
                <option value="15:15" <?php if ($row["fecha"] == '15:15:00') {echo 'disabled="disabled"';} ?>>3:15 P.M.</option>
                <option value="15:30" <?php if ($row["fecha"] == '15:30:00') {echo 'disabled="disabled"';} ?>>3:30 P.M.</option>
                <option value="15:45" <?php if ($row["fecha"] == '15:45:00') {echo 'disabled="disabled"';} ?>>3:45 P.M.</option>
                <option value="16:00" <?php if ($row["fecha"] == '16:00:00') {echo 'disabled="disabled"';} ?>>4:00 P.M.</option>
                <option value="16:15" <?php if ($row["fecha"] == '16:15:00') {echo 'disabled="disabled"';} ?>>4:15 P.M.</option>
                <option value="16:30" <?php if ($row["fecha"] == '16:30:00') {echo 'disabled="disabled"';} ?>>4:30 P.M.</option>
                <option value="16:45" <?php if ($row["fecha"] == '16:45:00') {echo 'disabled="disabled"';} ?>>4:45 P.M.</option>
                <option value="17:00" <?php if ($row["fecha"] == '17:00:00') {echo 'disabled="disabled"';} ?>>5:00 P.M.</option>
                <option value="17:15" <?php if ($row["fecha"] == '17:15:00') {echo 'disabled="disabled"';} ?>>5:15 P.M.</option>
                <option value="17:30" <?php if ($row["fecha"] == '17:30:00') {echo 'disabled="disabled"';} ?>>5:30 P.M.</option>
                <option value="17:45" <?php if ($row["fecha"] == '17:45:00') {echo 'disabled="disabled"';} ?>>5:45 P.M.</option>
                <option value="18:00" <?php if ($row["fecha"] == '18:00:00') {echo 'disabled="disabled"';} ?>>6:00 P.M.</option>
                <option value="18:15" <?php if ($row["fecha"] == '18:15:00') {echo 'disabled="disabled"';} ?>>6:15 P.M.</option>
                <option value="18:30" <?php if ($row["fecha"] == '18:30:00') {echo 'disabled="disabled"';} ?>>6:30 P.M.</option>
                <option value="18:45" <?php if ($row["fecha"] == '18:45:00') {echo 'disabled="disabled"';} ?>>6:45 P.M.</option>
                <option value="19:00" <?php if ($row["fecha"] == '19:00:00') {echo 'disabled="disabled"';} ?>>7:00 P.M.</option>
            <?php endforeach; ?>
            </select>
    
asked by Joaquín 28.08.2018 в 18:51
source

1 answer

2

Logically repeats the hours as many times as appointments you have registered. A quick and simple solution could be that you mount a non-associative array with the dates that the query returns, eliminate the loop and use the function in_array to compare the hours quoted with the drop-down time.

$id = '1';
$fechaEscogida = '2018-08-28';
$desde = '14:30:00';
$hasta = '19:00:00';
$sth = $conn->prepare("SELECT TIME(fecha) AS fecha FROM AGENDA WHERE id = 
'".$id."' AND DATE(fecha) = '".$fechaEscogida."' AND TIME(fecha) BETWEEN 
".$desde."' AND '".$hasta."'");
$sth->execute();
$results = $sth->fetchAll();
$horas = array();
foreach($results as $key => $value){
    $horas[] = $value['fecha'];
}

<select name="escogerHora">
    <option value="14:30" <?=(in_array('14:30:00', $horas)?'disabled':'')?>>2:30 P.M.</option>
</select>

I have set the example with one hour, with the others, the same process. There are better ways to do it but here is an answer without many changes in your code. Greetings!

    
answered by 29.08.2018 / 16:11
source