1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Time Slot

Discussion in 'PHP' started by jamie2011, Oct 20, 2010.

  1. #1
    Hi there,

    I know this probably isn't the most practical way but can someone help me out with my following code. The below code is for an appointments system, I have time slots within a day and the below code is grabbing the time value and matching it with the the time slot.

    So, if their is two records in the database of 09:00 and 09:15 times, it will show up on the time slot instructed and make the available link non-existent if the time exists. But here is where my problem is, FOR EACH record within the database, it will obviously echo out the two results, but due to no appointments being within the time slot of 09:30 and 09:45, these two slots echo twice because their is two records within the database when along with the result, I only want it to echo once within the time slot if the result matches the time.

    Following code out puts the follow result

    09:00 - Appointment Booked - by name - View Appointment
    09:15 - Available
    09:30 - Available
    09:45 - Available

    09:00 - Available
    09:15 - Appointment Booked - by name - View Appointment
    09:30 - Available
    09:45 - Available

    * The above example outputs the two rows with the matching time slots due to the while loop
    * This means time slots are echoed multiple times

    Want the solution to display the result in the following form

    09:00 - Appointment Booked - by name - View Appointment
    09:15 - Appointment Booked - by name - View Appointment
    09:30 - Available
    09:45 - Available

    * Would love for the time slots to echo once, and depending on the time, slot itself into its respective slot
    * Without the multiple slots echoing

    Is there a way to do this? and if so how?

    
    <?php
    
    include('connection.php'); 
    
    $comment = "SELECT *
    FROM profile
    INNER JOIN appointments
    ON profile.id=appointments.id WHERE date = '" .$_GET['date']."' ORDER BY date DESC LIMIT 5"; //MYSQL Query SELECT ALL FROM review WHERE id = row id
    
    $commentresult = mysql_query($comment);
    
    // If mysqlresult = false then query return fail
    if ($commentresult == false)
    {
    	die("Your Query isn't working correctly! :-( ");
    	
    }
    
    // If mysqlresult = true then echo below information with results
    if (mysql_num_rows($commentresult) >= 1)
    {
     while ($cr = mysql_fetch_array($commentresult)) //While Loop
    	{ 
    
    $time = $cr['time']; 
    if($time=="09:00") 
    { 
    
    // Show Appointment if database matches 09:00
    
        echo "<table border='0' width='800'>
    <tr><td width='35'><div class='content'>".$cr['time']."</div></td>
    <td width='100'><div class='content'>".$cr['status']."</div></td>
    <td width='100'><div class='content'>".$cr['firstname']." ".$cr['secondname']."</div></td>
    <td width='120'><div class='content'>View Appointment</div></td>
    </tr></table>";
    }
    
    else 
    { 
    // Show time slot is available if no appointments
    
       echo "<br><table border='0' width='800'>
    <tr><td width='100'><div class='content'>09:00</div></td>
    <td width='100'><div class='content'><a href='#'>Available</a></div></td>
    <td><div class='content'></div></td></tr></table><br>"; 
    } 
    
    if($time=="09:15") 
    { 
    
    // Show Appointment if database matches 09:15
        echo "<table border='0' width='800'>
    <tr><td width='35'><div class='content'>".$cr['time']."</div></td>
    <td width='100'><div class='content'>".$cr['status']."</div></td>
    <td width='100'><div class='content'>".$cr['firstname']." ".$cr['secondname']."</div></td>
    <td width='120'><div class='content'>View Appointment</div></td>
    </tr></table>";
    }
    
    else 
    { 
    // Show time slot is available if no appointments
    
       echo "<br><table border='0' width='800'>
    <tr><td width='100'><div class='content'>09:15</div></td>
    <td width='100'><div class='content'><a href='#'>Available</a></div></td>
    <td><div class='content'></div></td></tr></table><br>"; 
    } 
    
    if($time=="09:30") 
    { 
    
    // Show Appointment if database matches 09:30
        echo "<table border='0' width='800'>
    <tr><td width='35'><div class='content'>".$cr['time']."</div></td>
    <td width='100'><div class='content'>".$cr['status']."</div></td>
    <td width='100'><div class='content'>".$cr['firstname']." ".$cr['secondname']."</div></td>
    <td width='120'><div class='content'>View Appointment</div></td>
    </tr></table>";
    }
    
    else 
    { 
    // Show time slot is available if no appointments
    
       echo "<br><table border='0' width='800'>
    <tr><td width='100'><div class='content'>09:00</div></td>
    <td width='100'><div class='content'><a href='#'>Available</a></div></td>
    <td><div class='content'></div></td></tr></table><br>"; 
    } 
    
    if($time=="09:45") 
    { 
    
    // Show Appointment if database matches 09:45
        echo "<table border='0' width='800'>
    <tr><td width='35'><div class='content'>".$cr['time']."</div></td>
    <td width='100'><div class='content'>".$cr['status']."</div></td>
    <td width='100'><div class='content'>".$cr['firstname']." ".$cr['secondname']."</div></td>
    <td width='120'><div class='content'>View Appointment</div></td>
    </tr></table>";
    }
    
    else 
    { 
    // Show time slot is available if no appointments
    
       echo "<br><table border='0' width='800'>
    <tr><td width='100'><div class='content'>09:45</div></td>
    <td width='100'><div class='content'><a href='#'>Available</a></div></td>
    <td><div class='content'></div></td></tr></table><br>"; 
    } 	
    } // End Loop
    
    ?>
    
    PHP:
     
    Last edited: Oct 20, 2010
    jamie2011, Oct 20, 2010 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Please add some data from your profiles table so we can check that to!
     
    EricBruggema, Oct 21, 2010 IP
  3. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wooooooow
    Dude, sorry but you will need to seriously rethink your algo... Imagine if you had to do a full day's appointment every 5 min! Would you have a if ($time == "00:00")/if ($time == "00:05")/etc... ?

    I would do it like this:
    $start_time = mktime (9, 0, 0); // Start today at 9:00am
    echo "<table border='0' width='800'>";
    for ($i = 0; $i < 4; $i++)
    {
    	$time = strtotime ("+" . ($i * 15) . " minutes", $start_time); // That will give you each 15 min 9:00, 9:15, 9:30, 9:45
    
    	echo "<tr><td width='35'><div class='content'>" . date ("H:i", $time) . "</div></td>"; // Display the time
    
    	$comment = "SELECT *
    		FROM profile
    		INNER JOIN appointments
    		ON profile.id=appointments.id WHERE date = '" .$_GET['date']."' AND time = '" . date ("H:i", $time) . "";
    
    	$commentresult = mysql_query($comment);
    	if ($commentresult)
    	{
    		$cr = mysql_fetch_assoc($commentresult);
    		
    		echo "<td width='100'><div class='content'>".$cr['status']."</div></td>
    			<td width='100'><div class='content'>".$cr['firstname']." ".$cr['secondname']."</div></td>
    			<td width='120'><div class='content'>View Appointment</div></td>";
    	}
    	else
    	{
    		echo "<td width='100'><div class='content'><a href='#'>Available</a></div></td>
    			<td colspan='2'><div class='content'></div></td>";
    	}
    	echo "</tr>";
    }
    echo "</table>";
    PHP:
    Of course I didn't include any checks or anything, and I may well be out of context, but try to make sure you don't hardcode more data than you need.

    aXe
     
    axelay, Oct 22, 2010 IP
  4. sinha.sidhi

    sinha.sidhi Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    below code is right you can use this logic...
     
    sinha.sidhi, Aug 29, 2011 IP