0

下面的代码非常适合在行中列出每个类,我已经将它从日期和时间按 ASC 排序但现在我想重新排列它,以便星期一发生的类按开始时间 ASC 在第一列中再次按顺序显示然后继续循环,使周二发生的课程出现在第二列中,以此类推。我不确定最好的方法,或者即使我需要重写我的 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 时,我的 $data 来自 mysql db。

 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;
}
4

1 回答 1

0

您是从数据库中获取这些数据吗?如果是这样,您可能需要考虑更改您的查询以获得这种不同的分组。也许是这样的:

SELECT DAYOFWEEK(date_field) AS day_index, [... other class fields ...]
FROM table
ORDER BY day_index ASC;

然后你可以将你的数据读入一个二维数组,如下所示:

$calendar = array();
while ($row = $db_result->fetch() ) { // or whatever you use to iterate through the result set.
    $calendar[$row['day_index']][] = $row;
}

这会给你一个这样的数组:

Array(
    '1' => Array(
         0 =>  Array(
             'day_index' =>  '1',
             ... other class fields ...
         ),
         1 =>  Array(
              'day_index' => 1,
             ... other class fields ...
         ),
         ... other class arrays from the result set ...
         ),
    '2' => ... Another numerically indexed array of class arrays for Monday classes ...
    '3' => ... Another numerically indexed array of class arrays for Tuesday classes ...
    ... and so on
)

然后归结为您如何显示数据。我认为,为了做你想做的事,你可能需要放弃你拥有的基于表格的布局,而是使用浮动列之类的东西。

这可能看起来像这样::

<?php
$days_of_week = array(
    '1' => 'Sunday',
    '2' => 'Monday',
    // etc.
);
?>
<div style="overflow: auto;">
<?php foreach ($calendar as $day_index => $classes) { ?>
    <div style="width: 150px; margin: 5px; float: left;">
        <div style="margin: auto;"><?php echo $days_of_week[$day_index]; ?></div>
        <?php foreach ($classes as $class) {?>
            <div style="width:150px">
            <!-- class info here -->
            </div>
        <?php } ?>
    </div>
<?php } ?>
</div>
于 2013-01-15T19:14:09.620 回答