以下のコードは、各クラスを行にリストするのに最適で、日付と時刻から ASC で並べ替えられていますが、月曜日に発生するクラスが開始時刻の ASC で最初の列に再び順番に表示されるように並べ替えたいと思います。次に、火曜日に発生するクラスが列 2 に表示されるようにループを続けます。最善の方法がわからない、または foreach ステートメントを書き直す必要があるかどうかさえわかりません。それぞれのケースが「月曜日」、「火曜日」などの日に等しい文字列を使用して切り替えを試みました。
<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>
Mike B の SQL を使用する場合の mysql db からの my $data.
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
それを手に入れた:
$calendar = array();
foreach ($data as $row) {
$calendar[$row[0]['day_index']][] = $row;
}