I’m working on an online booking system and currently use a table bookings (id, slot id, treatment, user, date), treatments (id, name, description, duration), timeslots(id, time).
In the first page of booking you see treatment options, and a calender datepicker. Once you pick the calender date it sends this off to a php page and returns a list of available timeslot.
In this php page I have an array of booked times, an array of all the slots. The booked times are substracted from all available slots and the remaining array(all available slots) is then shown to the user so they see all times they can book an appointment.
Now I want to add a duration variable into the mix. This means that array of booked timeslots (say 3,5) and available timeslots(1,2,3,4,5,6,7,8,9,10) if the duration is bigger than two, the result of taking the first array off the second is (6,7,8,9,10). if the duration is 2, (1,2,6,7,8,9,10) and if it is 1 (1,2,4,6,7,8,9,10). Does anyone know how I can do such a thing?
Just for help with suggestions this is the php page:
<?php
include('connection.php');
error_reporting(E_ALL);
$treatment = $_POST['treatment'];
$bookdate = $_POST['bookdate'];
if(!isset($treatment) && isset($bookdate)){
$else = mysql_query("SELECT * FROM SLOTS");
echo '<SELECT>';
while($array_else = mysql_fetch_array($else)){
echo "<OPTION value=".$array_else['SL_ID'].">".$array_else['SL_TIME']."</OPTION>";
}
echo "</SELECT>";
}else{
$exp = explode("-", $bookdate);
//determine what day of the week it is
$timestamp = mktime(0,0,0,$exp[1],$exp[0],$exp[2]);
$dw = date( "w", $timestamp); // sun0,mon1,tue2,wed3,thur4,fri5,sat6
//find bookings with same date
$q1 = mysql_query("SELECT BOOK_SLOT_ID FROM BOOKINGS WHERE BOOK_DATE='$bookdate'");
//make array of booking slots
$array1 = array();
while ($s1 = mysql_fetch_array($q1)) {
$array1[] = $s1['BOOK_SLOT_ID'];
}
$q2 = mysql_query("SELECT SL_ID FROM SLOTS");
//make array of all slots
$array2 = array();
while ($s2 = mysql_fetch_array($q2)) {
$array2[] = $s2['SL_ID'];
}
if($dw == 6){
$q = mysql_query("SELECT SL_ID FROM SLOTS ORDER BY SL_ID DESC LIMIT 4");
$array = array();
while ($s = mysql_fetch_array($q)) {
$array[] = $s['SL_ID'];}
$array_res_merge = array_merge((array)$array1,(array)$array);
$arr_res = array_diff($array2, $array_res_merge);
}elseif($dw != 6){
//remove bookings from all slots
$arr_res = array_diff($array2, $array1);
}
echo '<div>';
//make selectable options of results
echo '<SELECT id="timeslots">';
foreach($arr_res as $op){
$r = mysql_query("SELECT SL_TIME FROM SLOTS WHERE SL_ID='$op'");
$q3 = mysql_fetch_array($r);
echo "<OPTION value=".$op.">".$q3['SL_TIME']."</OPTION>";
}
echo '</SELECT>';
echo'</div>';
}
?>
Any help with this issue is greatly appreciated.
I would like to point out that improving the description of your variables by giving better names would help read the code. I will point out the logic since it would take a while to figure out your variables…
Use array_slice to do the remove from the array. First param is the array, second param is the start key, and third param is the interesting one: it defines how many to take off.
To remove the items starting at the id and going backward, simply do this: