The code below works great for listing each class in rows and I’ve got it sorted ASC from date and time BUT now I’d like to rearrange it so the classes that occur on Monday appear in order again by start time ASC in column one then continue the loop so classes that occur on Tuesday appear in column two and so on through the week. I’m not sure of the best method or even if I need to rewrite my foreach statements. I’ve tried switch using strings where each case would equal the days “Monday”, “Tuesday”, etc.
<table>
<tr>
<th width="80">Day</th>
<th width="80">Class</th>
<th width="80">Instructor</th>
<th width="80">Start</th>
<th width="80">End</th>
<th width="80">Studio</th>
</tr>
<?php
foreach ($data as $key => $row)
{
$start_date[$key] = $row['Calendar']['start_date'];
$start_time[$key] = $row['Calendar']['start_time'];
}
array_multisort($start_date, $start_time, $data);
foreach ($data as $row) {
<tr>
<td><?php $row['Calendar']['start_date']; ?></td>
<td><?php $row['Calendar']['title']; ?></td>
<td><?php $row['Calendar']['instructor']; ?></td>
<td><?php $row['Calendar']['start_time']; ?></td>
<td><?php $row['Calendar']['end_time']; ?></td>
<td><?php $row['Calendar']['location']; ?></td>
</tr>
</table>


my $data from mysql db when using SQL from Mike B.
array(13) {
[0]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "2" } ["calendars"]=> array(4) { ["title"]=> string(19) "Breakaway (Erin K.)" ["start_time"]=> string(8) "12:15:00" ["end_time"]=> string(8) "13:00:00"
["location"]=> string(28) "Studio 1" } }
[1]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "2" } ["calendars"]=> array(4) { ["title"]=> string(20) "Breakaway (Cindy B.)" ["start_time"]=> string(8) "16:30:00" ["end_time"]=> string(8) "17:15:00"
["location"]=> string(28) "Studio 1" } }
[2]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "2" } ["calendars"]=> array(4) { ["title"]=> string(20) "Breakaway (Larry N.)" ["start_time"]=> string(8) "18:15:00" ["end_time"]=> string(8) "19:00:00"
["location"]=> string(28) "Studio 1" } }
[3]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "3" } ["calendars"]=> array(4) { ["title"]=> string(20) "Breakaway (Megan S.)" ["start_time"]=> string(8) "06:45:00" ["end_time"]=> string(8) "07:30:00"
["location"]=> string(28) "Studio 1" } }
[4]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "3" } ["calendars"]=> array(4) { ["title"]=> string(20) "Breakaway (Larry N.)" ["start_time"]=> string(8) "11:15:00" ["end_time"]=> string(8) "12:00:00"
["location"]=> string(28) "Studio 1" } }
[5]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "3" } ["calendars"]=> array(4) { ["title"]=> string(19) "Breakaway (Lisa G.)" ["start_time"]=> string(8) "17:30:00" ["end_time"]=> string(8) "18:15:00"
["location"]=> string(28) "Studio 1" } }
[6]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "4" } ["calendars"]=> array(4) { ["title"]=> string(20) "Breakaway (Megan S.)" ["start_time"]=> string(8) "16:30:00" ["end_time"]=> string(8) "17:15:00"
["location"]=> string(28) "Studio 1" } }
[7]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "4" } ["calendars"]=> array(4) { ["title"]=> string(19) "Breakaway (Lisa G.)" ["start_time"]=> string(8) "12:15:00" ["end_time"]=> string(8) "13:00:00"
["location"]=> string(28) "Studio 1" } }
[8]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "5" } ["calendars"]=> array(4) { ["title"]=> string(21) "Breakaway (Lauren M.)" ["start_time"]=> string(8) "06:45:00" ["end_time"]=> string(8) "07:30:00"
["location"]=> string(28) "Studio 1" } }
[9]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "5" } ["calendars"]=> array(4) { ["title"]=> string(19) "Breakaway (Adam A.)" ["start_time"]=> string(8) "11:15:00" ["end_time"]=> string(8) "12:00:00"
["location"]=> string(28) "Studio 1" } }
[10]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "5" } ["calendars"]=> array(4) { ["title"]=> string(20) "Breakaway (Cindy B.)" ["start_time"]=> string(8) "17:30:00" ["end_time"]=> string(8) "18:15:00"
["location"]=> string(28) "Studio 1" } }
[11]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "6" } ["calendars"]=> array(4) { ["title"]=> string(19) "Breakaway (Adam A.)" ["start_time"]=> string(8) "16:30:00" ["end_time"]=> string(8) "17:15:00"
["location"]=> string(28) "Studio 1" } }
[12]=> array(2) { [0]=> array(1) { ["day_index"]=> string(1) "7" } ["calendars"]=> array(4) { ["title"]=> string(20) "Breakaway (Rotation)" ["start_time"]=> string(8) "09:35:00" ["end_time"]=> string(8) "10:35:00"
["location"]=> string(28) "Studio 1" } } }
SQL
SELECT DAYOFWEEK(start_date) as day_index, title, start_time, end_time, location FROM calendars WHERE calendar_category_id = '3' AND location = 'Studio 1' ORDER BY day_index
got it by:
$calendar = array();
foreach ($data as $row) {
$calendar[$row[0]['day_index']][] = $row;
}
Are you getting this data from a database? If so you might want to consider changing up your query to get this different grouping. Perhaps something like this:
Then you could read your data into a two-dimensional array like this:
This gives you an array like this:
Then it comes down to how you are displaying your data. I would think that in order to do what you want to do, you might need to go away from the table based layout you have and instead go with something like floated columns.
This might look like this::