Jump to content

IF statement within Loop


greenApple

Recommended Posts

I am trying to create an appointment booking system, I have a loop that creates 15 minute intervals which a user can select. Once a user selects a time, that time should then become unavailable for anyone else to select. Treatment lengths vary in time - 15 and 30 minutes. The problem was that I was not been able to create an IF statement within the loop that would show unavailable slots for treatments which have a longer length than the loop interval - in this case 15 mins.I have been able to create an IF statement that will show two unavailable slots for 30 minute treatments, however now I have an issue where the time selected shows within the tables as unavailable but a link breaks out of the table and displays as available. The image below shows what I am talking about, 9:00 is a 15 min treatment so this displays correctly, 10:00 has been selected as a 30 min treatment, so 10:00 and 10:15 display as unavailable - this is what I want, but above the 9:00 and 10:00 show an available link which selects 9:00 and 10:00 if selected. How can I stop this link displaying?if3.pngThis is my current code for the loop

<?php    		$queryLength = mysql_query("SELECT treatmentLength FROM treatment WHERE treatment='$treatment'");				while ($row = mysql_fetch_assoc($queryLength))//place into an array called $row			{				$dblength = $row['treatmentLength'];//this is the length of the treatment from Db			}	        //create loop time variables	//start time 9am	$hour=9;	$minute=00;	//end time 5pm	$endHour=17;	$endMinute=00;		//utc of start and end dates	$start=mktime($hour,$minute,0, $month, $day, $year);	$end=mktime($endHour,$endMinute,0, $month, $day, $year);		//create daily timetable table	while($start<=$end)		{			echo"<table border='1'>";			echo"<tr>";				echo"<td>";					echo date('H:i',$start)."<br />";					$s= date('Y/m/d H:i:s',$start);//put into SQL format									echo"</td>";				echo"<td>";		if ($treatment)	{		$query = mysql_query("SELECT startTime , endTime FROM appointment WHERE startTime='$s'");		$numrows = mysql_num_rows($query);			while ($row = mysql_fetch_assoc($query))//create time array			{				$dbstartTime = $row['startTime'];				$dbendTime = $row['endTime'];			}						if ($numrows!=0)				{						echo "Unavailable";//displays unavailable for start time					echo"</td>";				}							else                                //check end time of the treatment				$next=$start+900; //$next is 30 minutes				$strEnd = strtotime($dbendTime);//change length from SQL format				if($strEnd == $next)					{							echo "<td>";						echo "Un";//displays unavailable in the next 15 min table row, below the start table row						echo "</td>";					}								else						echo "<a href='test3.php?appointmentTime=$start&treatment=$treatment&length=$dblength'>Available</a>";				echo"</td>";										$start=$start+900; //run loop again, increment time by 900 seconds(15 mins)		}			else			die("Please Select treatment");				echo"</td>";			echo"</tr>";			echo "</table>";		}?>

I have been working on this for over the past week, but I can't get any further with it :):):) . I need someone with more experience and fresh eyes to have a look and see what I am doing wrong! Any help would be great!!!!!!!

Link to comment
Share on other sites

It looks like your else statements are missing brackets. You should change your SQL query though so that for each time slot you're looking for either an appointment that starts at that time, or an appointment that started earlier and has a longer length that would make it take up that time slot. All of that should be in the SQL statement.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...