Jump to content

Need assistance with arrays & loops


Hobbyist_PHPer

Recommended Posts

Hello everyone...

 

I'm hoping someone could help with me an issue I'm having... One that I seem to come across quite regularly, and yet have never come up with a good solution...

 

In this particular case I have an array ... Here's a partial view of it:

$CalendarTimes = Array
(
    "12:00 am" => "12:00 am",
    "12:15 am" => "12:15 am",
    "12:30 am" => "12:30 am",
    "12:45 am" => "12:45 am",
    "1:00 am" => "1:00 am",
    "1:15 am" => "1:15 am",
    etc. etc. etc.

 

Now let's say that I have a database table with appointment times... How do I match up the results of the query from the database to the corresponding values in the array? (without creating a loop inside of a loop, which is pretty much useless as I have found out)

 

Here's my query and while loop:

$BeginningDateTime = date('Y-m-d 00:00:00', strtotime($SelectedDay));
$EndingDateTime = date('Y-m-d 23:59:59', strtotime($SelectedDay));
$query = "SELECT OrderTickets.*, Appointments.* FROM Appointments 
    LEFT JOIN OrderTickets ON Appointments.OrderTicketID = OrderTickets.OrderTicketID 
    WHERE Appointments.ExaminerID = '{$_SESSION['ExaminerID']}' 
    AND Appointments.AppointmentStartDateTime BETWEEN '$BeginningDateTime' AND '$EndingDateTime'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
    $theBeginningTime = date('H:i:s', strtotime($row['AppointmentStartDateTime']));
    $theEndingTime = date('H:i:s', strtotime($row['AppointmentFinishDateTime']));

}

 

P.S. I know about my deprecated mysql functions

Link to comment
Share on other sites

You query for the data you want, then you store all the records/row-arrays in a php array using the datetime of the appointment as the array index/key (so that you can find the correct record easily later.) As you are looping to produce the output for each time slot, you form a datatime value of that time slot and use that value to test for/retrieve the record/row for that time slot from the php array you created.

Link to comment
Share on other sites

You query for the data you want, then you store all the records/row-arrays in a php array using the datetime of the appointment as the array index/key (so that you can find the correct record easily later.) As you are looping to produce the output for each time slot, you form a datatime value of that time slot and use that value to test for/retrieve the record/row for that time slot from the php array you created.

 

That worked, thank you.

Link to comment
Share on other sites

Here's the code that I used...

 

$BeginningDateTime = date('Y-m-d 00:00:00', strtotime($SelectedDay));
$EndingDateTime = date('Y-m-d 23:59:59', strtotime($SelectedDay));
$query = "SELECT OrderTickets.*, Appointments.* FROM Appointments LEFT JOIN OrderTickets ON Appointments.OrderTicketID = OrderTickets.OrderTicketID WHERE Appointments.ExaminerID = '{$_SESSION['ExaminerID']}' AND Appointments.AppointmentStartDateTime BETWEEN '$BeginningDateTime' AND '$EndingDateTime'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
    $theBeginningTime = date('g:i a', strtotime($row['AppointmentStartDateTime']));
    $theEndingTime = date('g:i a', strtotime($row['AppointmentFinishDateTime']));
    $DatabaseResultsArray[$theBeginningTime] = array(
        $theBeginningTime => array($row['OrderTicketID'],$theEndingTime,$row['WhatsHappening'])
    );
}
foreach ($CalendarTimes as $value) 
{
    $rowCounter += 1;
    echo '<tr class="'.OddOrEven2($rowCounter).'">';
    echo '<td class="time">'.$value.'</td>';
    echo '<td>';
    foreach ($DatabaseResultsArray as $subvalue => $key)
    {
        if ($value == $subvalue)
        {
            echo $key[$subvalue][0].'<br />';
            echo $key[$subvalue][1].'<br />';
            echo $key[$subvalue][2].'<br />';
        }
    }
    echo '</td>';
    echo '</tr>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.