I simply need to check from database if Day is Blocked show in a select box as disabled and if NOT show the next 2 months all the days in the same select box.
I get the Closed(Blocked) days with no problem but when i am trying to get the rest of the days i get everything double.If NOT else for arrays not working.
The results show double like below :
Thursday, 30 Mar ,
Thursday, 30 Mar
and even the DISABLED day from the first loop shows double after looping.
Further clarification:
I am using the For Loop to get the next 2 months of dates. I am using the foreach loop to get the array with the days that are closed (Sunday, Monday)… So i want to show in the select box (Dates with Sunday or Monday disabled) and show the rest of the days Normal.
My sql
CREATE TABLE `check_availability` (
`id` int(10) NOT NULL,
`Day` varchar(10) NOT NULL,
`Open_hour` varchar(10) NOT NULL,
`Closed_hour` varchar(10) NOT NULL,
`ClosedDays` varchar(10) NOT NULL,
`Blocked` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `check_availability` (`id`, `Day`, `Open_hour`, `Closed_hour`, `ClosedDays`, `Blocked`) VALUES
(1, 'Sunday', '09:00am', '10:00pm', '', 1),
(2, 'Monday', '09:00am', '10:00pm', '', 1);
Mysqli fetch code. The array is correct. Array ( [0] => Array ( [Day] => Sunday ) [1] => Array ( [Day] => Monday ) )
On looping through the disabled days gets me all the dates which are Sunday or Monday for 2 months correctly.
$sqlClosedDays = "SELECT Day FROM check_availability WHERE Blocked ='1'";
$resultClosed = mysqli_query($con, $sqlClosedDays);
$DaysClosed = mysqli_fetch_all($resultClosed,MYSQLI_ASSOC);
My for and Foreach loop that cause the dates to show double.
echo "<select name='add_r_date' id='add_r_date'>";
for($i=0;$i<=60;$i++){
//Get the Closed Days
//Display the Days according to limit
$day=date('l,d M',strtotime("+$i day"));
$InsDay = date('Y-m-d',strtotime("+$i day"));
foreach ($DaysClosed as $rowDaysClosed) {
$dayClosed = $rowDaysClosed['Day'];
$dayClosedTrim= substr($dayClosed, 0, 3);
$dayTrim = substr($day, 0, 3);
//If not Closed set as available
if ($dayTrim === $dayClosedTrim ) {
echo "<option disabled value='$InsDay'>$day</option> ";
//$clDays[] = $day;
} else {
echo "<option name='add_r_date' id='add_r_date' value='$InsDay'>$day</option>";
}
}//EOF FOREACH LOOP
}//EOF FOR LOOP
echo "</select>";
I tried unset($day); or unset($dayClosed);
I tried Break; inside the loops but the problem still there.
The problem it that the values show 2 times. I just want to show in disabled the days that are closed(Sunday) and (Monday) and show the rest days normal so that the people can select available dates to make reservation.
2
Answers
The problem solved thanks to @CBroe suggestion to add boolean flags before the loop and inside the loop. After i use those flags to check . Also i added Breaks to stop the looping. Lastly i added on the beginning of the code a check to the database if there exists any closing days.If not it outputs all the dates according to the for loop. Here is the working code.
You can use DISTINCT method :
or you can use GROUP BY method :