You're facing an issue with dynamic select box options based on data from your MySQL database. You want to display time slots that are available and disable the ones that have been booked already.
To solve this problem, you can use a single query to fetch all time slots and then iterate through the result set. For each time slot, check if it's booked by joining the tb_event_time_slots
table with the tb_event_booking
table on the date specified in the calendar. If a time slot is booked, add a disabled attribute to the option.
Here's an updated PHP code snippet:
$calendar .= "<select name='seltimeslot' class='seltime'>";
$calendar .= "<option value=''>Select a Time</option>";
$sql = "SELECT * FROM `tb_event_time_slots` ORDER BY `event_time_slot_id`";
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs)) {
$is_booked = false;
$sql2 = "SELECT * FROM `tb_event_booking` WHERE `event_date` = '$event_date' AND `event_time_slot_id` = '" . $row['event_time_slot_id'] . "'";
$rs2 = mysql_query($sql2);
while ($row2 = mysql_fetch_array($rs2)) {
if (!empty($row2)) {
$is_booked = true;
break;
}
}
if (!$is_booked) {
$calendar .= "<option value='" . $row['event_time_slot_id'] . "'>" . $row['event_time_slots'] . "</option>";
} else {
$calendar .= "<option value='" . $row['event_time_slot_id'] . "' disabled='disabled'>" . $row['event_time_slots'] . "</option>";
}
}
$calendar .= "</select>";
In this code, we first fetch all time slots from the database. Then, for each time slot, we check if it's booked by querying the tb_event_booking
table with the specified date and time slot ID. If a time slot is booked, we add a disabled attribute to the option.
Using PHP to Remove a Time Slot after it was Booked
To remove a time slot as an option once it has been booked, you can use a similar approach. When a reservation is made, update the tb_event_time_slots
table by setting a flag indicating that the time slot is no longer available.
Here's an updated PHP code snippet for removing a time slot after it was booked:
if ($delete_reservation) {
// Update the tb_event_time_slots table to remove the booked time slot
$sql = "UPDATE `tb_event_time_slots` SET `available` = 0 WHERE `event_time_slot_id` = '" . $reservation['roomid'] . "' AND `start_time` BETWEEN '$start_time' AND '$end_time'";
mysql_query($sql);
}
In this code, we assume that the delete_reservation
variable is set to true when a reservation is deleted. We then update the tb_event_time_slots
table by setting the available
flag to 0 for the booked time slot.
When displaying the available time slots in the calendar, you can use the same approach as before: fetch all time slots from the database and iterate through the result set. This time, you'll only display the time slots that are available (i.e., have an available
flag of 1).
I hope this helps! Let me know if you have any further questions.