1

I have to relations: one with the times and one where the selected times go in after submitting a form. I'm trying to create a dropdown menu, which should be sorted. If a time slot is already occupied is should still show up in the drop down, but as disabled. E.G. 9 , 10: occupied, 11... and so on. At the moment the occupied slots are at the bottom of the menu. How can I achieve, that they appear where they should be. Here is my code so far:

$query = "SELECT stunde FROM zeiten WHERE buchbar = 2 and
NOT EXISTS (SELECT *
FROM raumbuchung
WHERE zeiten.stunde =
raumbuchung.zeitanfang and belegt = 'belegt');
SELECT zeitanfang, belegt from
raumbuchung where belegt = 'belegt'";

echo "Beginn der Veranstaltung: ";
echo "<select name='time' id='t1'>";
if (mysqli_multi_query($conn, $query)) {
    do {
        if ($result = mysqli_store_result($conn)) {
            while ($row = mysqli_fetch_assoc($result)) {
                if ($row[belegt]) {
                echo "<option value=$row[zeitanfang] disabled>$row[zeitanfang]: $row[belegt]</option>";
                }
                else {
                    echo "<option value=$row[stunde]>$row[stunde]</option>";
                }
            }
        }
    }
        while(mysqli_next_result($conn)); 
        }

Maybe someone can help me out?

Aldi Kasse 2
  • 143
  • 1
  • 13

1 Answers1

0

First store them in separate arrays then populate as your wish.

$query = "SELECT stunde FROM zeiten WHERE buchbar = 2 and
NOT EXISTS (SELECT *
FROM raumbuchung
WHERE zeiten.stunde =
raumbuchung.zeitanfang and belegt = 'belegt');
SELECT zeitanfang, belegt from
raumbuchung where belegt = 'belegt'";
$occupied_arr = array();
$available_arr = array();
if (mysqli_multi_query($conn, $query)) {
    do {
        if ($result = mysqli_store_result($conn)) {
            while ($row = mysqli_fetch_assoc($result)) {
                if ($row['belegt']) {
                    $occupied_arr[] = $row;
                }
                else {
                    $available_arr[] = $row;
                }
            }
        }
    }
    while(mysqli_next_result($conn));
 }

echo "Beginn der Veranstaltung: ";
echo "<select name='time' id='t1'>";
foreach ($available_arr as $key => $value) {
    echo "<option value=".$value['stunde'].">".$value['stunde']."</option>";
}
foreach ($occupied_arr as $key => $value) {
    echo "<option value=".$value['zeitanfang']."disabled>".$value['zeitanfang'].": ".$value['belegt']."</option>";
}
echo "</select>";
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71