technotool Posted January 30, 2009 Share Posted January 30, 2009 HI i'm having a problem with a loop within a loop concept. First, I save appointments in a mysql database with their associated unix timestamp. I save this in a variable called $scheduledtimestamp. I can pull the appointments with a while loop. Next, I list the appointments with a table that has a start time (again with a unix timestamp which I convert to human time with the date function) and loop through appointment blocks by adding the appropriate amount of seconds and then echo the unix timestamp for the start of the next appointment. Each appointment starttime is saved as a variable $timestamp 08:00 appointment 08:15 appointment 08:30 appointment 08:45 appointment 09:00 appointment I use an if statement to check if ($timestamp== $scheduledtimestamp(from database) ){ then echo appointment in table} My problem is that I cannot seem to make the while loop work inside the for loop so that whenever there is a scheduled appointment with variable $scheduledtimestamp == display table variable $timestamp ==> the table displays the appropriate appointment message. What is happening now is that the table only lists the last $scheduledtimestamp value which is equal to the table $timestamp and then displays only one appointment in the table. I suspect because the the two strings above are not being checked at each appointment block appropriately. Please check the while loop in the for loop. This current way also makes a mysql request at every iteration of the for loop...costly!!! There must be a better way..basically, I am thinking that there must be a way to check all the $scheduledtimestamp values in an array from the database and compare the array values(loop) with each $timestamp. if there is a hit, then echo the appointment. <? //get the date from the calendar $appointment_date= $_GET['thedate']; echo "appointment date: "; $timestampfromstring=strtotime($appointment_date); $monthfromstring=date("m", $timestampfromstring); $dayfromstring=date("d", $timestampfromstring); $yearfromstring=date("Y", $timestampfromstring); $month=$monthfromstring; $day=$dayfromstring; $year=$yearfromstring; $appointmentblock_time_min=15; //appointment blocks $start_timestamp=mktime(8,0,0,$month,$day,$year); //start time echo $starttimestringthis=date("m-d-y H:i:s", $start_timestamp); $appointments=30; $personId=$_SESSION['person_id']; echo "<table>"; echo "<strong>Patient: ".$_SESSION['patient_lastname'].", ".$_SESSION['patient_firstname']."  DOB:".$_SESSION['patient_dob']."$nbsp ID:".$_SESSION['person_id']."</strong><br><br>"; for ($i=0; $i<$appointments; $i++){ echo "<tr>"; echo "<td>"; echo $timestamp = $start_timestamp + (60*$appointmentblock_time_min)*($i); //echo date("H:i", $timestamp); echo "<br>"; // list the appointments that have already been scheduled $query="SELECT * FROM calendar"; $result=mysql_query($query); while ($rows=mysql_fetch_assoc($result)){ $scheduledid=$rows['id']; $scheduledappointment=$rows['appointment']; $scheduledtimestamp=$rows['timestamp']; $scheduleduserid=$rows['userid']; } if ($timestamp== $scheduledtimestamp){ echo "</td>"; echo "<td>$scheduledappointment"; echo "</td>"; echo "</tr>"; } else{ echo "</td>"; echo "<td id=\"$timestamp\" onmouseover=\"this.style.background='#FECE6E';\" onmouseout=\"this.style.background='#FFF'; \" onclick=\"setAppointment(this.id,$personId,setObj()); \">"; echo "appointment"; echo "</td>"; echo "</tr>"; } } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/143085-loop-within-loop-for-appointment-schedule-app/ Share on other sites More sharing options...
manny Posted April 1, 2009 Share Posted April 1, 2009 HI i'm having a problem with a loop within a loop concept. First, I save appointments in a mysql database with their associated unix timestamp. I save this in a variable called $scheduledtimestamp. I can pull the appointments with a while loop. Next, I list the appointments with a table that has a start time (again with a unix timestamp which I convert to human time with the date function) and loop through appointment blocks by adding the appropriate amount of seconds and then echo the unix timestamp for the start of the next appointment. Each appointment starttime is saved as a variable $timestamp 08:00 appointment 08:15 appointment 08:30 appointment 08:45 appointment 09:00 appointment I use an if statement to check if ($timestamp== $scheduledtimestamp(from database) ){ then echo appointment in table} My problem is that I cannot seem to make the while loop work inside the for loop so that whenever there is a scheduled appointment with variable $scheduledtimestamp == display table variable $timestamp ==> the table displays the appropriate appointment message. What is happening now is that the table only lists the last $scheduledtimestamp value which is equal to the table $timestamp and then displays only one appointment in the table. I suspect because the the two strings above are not being checked at each appointment block appropriately. Please check the while loop in the for loop. This current way also makes a mysql request at every iteration of the for loop...costly!!! There must be a better way..basically, I am thinking that there must be a way to check all the $scheduledtimestamp values in an array from the database and compare the array values(loop) with each $timestamp. if there is a hit, then echo the appointment. <? //get the date from the calendar $appointment_date= $_GET['thedate']; echo "appointment date: "; $timestampfromstring=strtotime($appointment_date); $monthfromstring=date("m", $timestampfromstring); $dayfromstring=date("d", $timestampfromstring); $yearfromstring=date("Y", $timestampfromstring); $month=$monthfromstring; $day=$dayfromstring; $year=$yearfromstring; $appointmentblock_time_min=15; //appointment blocks $start_timestamp=mktime(8,0,0,$month,$day,$year); //start time echo $starttimestringthis=date("m-d-y H:i:s", $start_timestamp); $appointments=30; $personId=$_SESSION['person_id']; echo "<table>"; echo "<strong>Patient: ".$_SESSION['patient_lastname'].", ".$_SESSION['patient_firstname']."  DOB:".$_SESSION['patient_dob']."$nbsp ID:".$_SESSION['person_id']."</strong><br><br>"; for ($i=0; $i<$appointments; $i++){ echo "<tr>"; echo "<td>"; echo $timestamp = $start_timestamp + (60*$appointmentblock_time_min)*($i); //echo date("H:i", $timestamp); echo "<br>"; // list the appointments that have already been scheduled $query="SELECT * FROM calendar"; $result=mysql_query($query); while ($rows=mysql_fetch_assoc($result)){ $scheduledid=$rows['id']; $scheduledappointment=$rows['appointment']; $scheduledtimestamp=$rows['timestamp']; $scheduleduserid=$rows['userid']; } if ($timestamp== $scheduledtimestamp){ echo "</td>"; echo "<td>$scheduledappointment"; echo "</td>"; echo "</tr>"; } else{ echo "</td>"; echo "<td id=\"$timestamp\" onmouseover=\"this.style.background='#FECE6E';\" onmouseout=\"this.style.background='#FFF'; \" onclick=\"setAppointment(this.id,$personId,setObj()); \">"; echo "appointment"; echo "</td>"; echo "</tr>"; } } echo "</table>"; ?> Well you had it right but just the wrong order... Try replacing your code with this one( note the changes ) <?php //get the date from the calendar $appointment_date= $_GET['thedate']; echo "appointment date: "; $timestampfromstring=strtotime($appointment_date); $monthfromstring=date("m", $timestampfromstring); $dayfromstring=date("d", $timestampfromstring); $yearfromstring=date("Y", $timestampfromstring); $month=$monthfromstring; $day=$dayfromstring; $year=$yearfromstring; $appointmentblock_time_min=15; //appointment blocks $start_timestamp=mktime(8,0,0,$month,$day,$year); //start time echo $starttimestringthis=date("m-d-y H:i:s", $start_timestamp); $appointments=30; $personId=$_SESSION['person_id']; ///Here we build the array $query="SELECT * FROM calendar"; $result=mysql_query($query); $xx=0; while ($rows=mysql_fetch_assoc($result)) { $scheduledid[$xx]=$rows['id']; $scheduledappointment[$xx]=$rows['appointment']; $scheduledtimestamp[$xx]=$rows['timestamp']; $scheduleduserid[$xx]=$rows['userid']; $xx++; } echo "<table>"; echo "<strong>Patient: ".$_SESSION['patient_lastname'].", ".$_SESSION['patient_firstname']."  DOB:".$_SESSION['patient_dob']."$nbsp ID:".$_SESSION['person_id']."</strong><br><br>"; //for ($i=0; $i<$appointments; $i++) $i=0; foreach ($scheduledid as $res_id=>$row_id) { if ($i > $appointments) { continue; } echo "<tr>"; echo "<td>"; echo $timestamp = $start_timestamp + (60*$appointmentblock_time_min)*($i); //echo date("H:i", $timestamp); echo "<br>"; // list the appointments that have already been scheduled if ( in_array($timestamp,$scheduledtimestamp)) { echo "</td>"; echo "<td>$scheduledappointment[$res_id]"; echo "</td>"; echo "</tr>"; } else { echo "</td>"; echo "<td id=\"$timestamp\" onmouseover=\"this.style.background='#FECE6E';\" onmouseout=\"this.style.background='#FFF'; \" onclick=\"setAppointment(this.id,$personId,setObj()); \">"; echo "appointment"; echo "</td>"; echo "</tr>"; } $i++; } echo "</table>"; ?> oh and let me know if it worked out for you Link to comment https://forums.phpfreaks.com/topic/143085-loop-within-loop-for-appointment-schedule-app/#findComment-798687 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.