spearchilduser Posted December 10, 2012 Share Posted December 10, 2012 <?php @require_once("includes/dbconfig.inc"); $day=$_GET["myday"]; //to get the day from the previous page $month=$_GET["mymonth"];//to get the month from the previous page $year=$_GET["myyear"]; //to get the year from the previous page $insertdate=date("Y-m-d",mktime(0,0,0,$month,$day,$year)); //to convert from d-m-y to m-d-y for SQL table $practiceid=$_GET["mypractice"]; //to get the year from the previous page // Starting of top line showing name of the day's slots echo"<table align=center width='100' border='1'>"; echo "<tr><td><b>Slots</b></td><tr>"; // Starting of the slots for($i=0;$i<=31;$i++){ // this shows 32 x 15 minute slots from 9:00am - 17:00pm $shr=9+floor($i/4); //calculates the start hour of a slot $smin=($i%4)*15; //calculates the start hour of a slot $displayTime=$shr.":".$smin; // this displays the full start time of the 15 minute slot echo "<tr><td valign=top>"; $dbslottimes = mysql_query("SELECT Appointment_Time FROM appointment WHERE Appointment_Date='2012-12-29'"); // query to select all the times in the appointment table where the appoouintment date is 29/12/2012 $dbslottimes1 = mysql_num_rows($dbslottimes); while($row = mysql_fetch_array($dbslottimes)){ // while loop to check the values that the query collected $app1 = $row["Appointment_Time"]; // storing the database field from the query into a variable to be used in the while loop //echo $app1; // this will print the values of those queries } if ($dbslottimes == "") // if statement to vheck the values in the database match the slots // this will then be used to only display the ones that do not match { echo $displayTime."<br>"; // if there is a match it willl just display the slot as a time } else { echo "<a href=dateinsert.php?day=$day&month=$month&year=$year&hour=$shr&min=$smin&practice=$practiceid>"; echo $displayTime."<br>"; //show the clickable link for the slot if there is not a match with the if statement. } echo "</td></tr>"; } echo "</table>"; ?> this code is to show a number of "slots" from 9 o clock and one every 15 minutes untill somethign liek 5 o clock. What i need to do is check the database if there is a time already in the database then to show that slot as a non link and show the others as links. i need help with the if statement that is not complete atm . cheers Quote Link to comment https://forums.phpfreaks.com/topic/271799-help-with-calendar-please/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2012 Share Posted December 10, 2012 There's almost never a reason (good or bad) to perform queries inside of a loop. You need to - 1) Execute one query that gets the records you need for one complete page. If you are trying to display data for one day, you would retrieve all the records that match that year/month/day. If you are trying to display data for one month, you would retrieve all the records that match that year/month. 2) Fetch all the records that the query matched and store them into a php array, using the datetime of the record as the array key. If you can have more than one 'event' on any datetime, you would make a two-dimensional array. $data = array(); while($row = mysql_fetch_assoc($dbslottimes)){ // if there can be only one event per datetime - $data[$row['your_datetime_field_here...']] = $row; // if there can be more than one event per datetime - $data[$row['your_datetime_field_here...']][] = $row; } 3) As you loop through the datetime slots to produce your output, form a datetime value of that datetime slot (in $datetime for the following sample code) and use that value to test/reference any data in the array you formed in step #2. To just test if there is data, you can use isset($data[$datetime]) or array_key_exists($datetime,$data). To reference the data - // if there can be only one event per datetime - if(isset($data[$datetime])){ echo $data[$datetime]['some_field_name_here...']; } // if there can be more than one event per datetime - if(isset($data[$datetime]) && is_array($data[$datetime])){ foreach($data[$datetime] as $row){ echo $row['some_field_name_here...']; } } Quote Link to comment https://forums.phpfreaks.com/topic/271799-help-with-calendar-please/#findComment-1398469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.