Hobbyist_PHPer Posted September 8, 2012 Share Posted September 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268154-need-assistance-with-arrays-loops/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 8, 2012 Share Posted September 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268154-need-assistance-with-arrays-loops/#findComment-1376313 Share on other sites More sharing options...
Jessica Posted September 8, 2012 Share Posted September 8, 2012 Why do you have an array of calendar times that looks like that? Quote Link to comment https://forums.phpfreaks.com/topic/268154-need-assistance-with-arrays-loops/#findComment-1376314 Share on other sites More sharing options...
Hobbyist_PHPer Posted September 8, 2012 Author Share Posted September 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268154-need-assistance-with-arrays-loops/#findComment-1376324 Share on other sites More sharing options...
Hobbyist_PHPer Posted September 8, 2012 Author Share Posted September 8, 2012 Why do you have an array of calendar times that looks like that? I thought it would easier to build the array and call it rather than recreating it every time like I did in the other post that you helped me with... Quote Link to comment https://forums.phpfreaks.com/topic/268154-need-assistance-with-arrays-loops/#findComment-1376325 Share on other sites More sharing options...
Hobbyist_PHPer Posted September 8, 2012 Author Share Posted September 8, 2012 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/268154-need-assistance-with-arrays-loops/#findComment-1376326 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.