使用多个foreach显示计划。如何改进代码并减少foreach循环?

时间:2014-05-25 13:21:05

标签: php arrays foreach calendar schedule

我试图从数据库中制作一个如下所示的计划:enter image description here

我最终使用了三个循环进行显示。首先是显示日期,第二个是将每天分成重叠事件的行,第三个是连续打印事件。

在显示之前,我有两个数组:$overlap_array$dates_unique$overlap_array是具有已解决行的数组,其中包含重叠的事件。以下是$overlap_array的小片段:

array (size=7)
0 => 
array (size=2)
  0 => 
    array (size=7)
      0 => string '2014-04-21' (length=10)
      1 => string '10:00:00' (length=8)
      2 => string '14:00:00' (length=8)
      3 => string '36000' (length=5)
      4 => string '14400' (length=5)
      5 => string 'Listening Cities Presentations Session 1' (length=40)
      6 => string '1' (length=1)
  1 => 
    array (size=7)
      0 => string '2014-04-21' (length=10)
      1 => string '14:00:00' (length=8)
      2 => string '20:30:00' (length=8)
      3 => string '50400' (length=5)
      4 => string '23400' (length=5)
      5 => string 'Listening Cities Presentation 2' (length=31)
      6 => string '2' (length=1)
1 => 
array (size=3)
  0 => 
    array (size=7)
      0 => string '2014-04-21' (length=10)
      1 => string '15:30:00' (length=8)
      2 => string '17:00:00' (length=8)
      3 => string '55800' (length=5)
      4 => string '5400' (length=4)
      5 => string 'Listening Cities Performance at the DockBar: Part1' (length=50)
      6 => string '3' (length=1)

$dates_unique是一个在日程安排中具有唯一日期的数组:

array (size=3)
  0 => string '2014-04-21' (length=10)
  6 => string '2014-04-22' (length=10)
  9 => string '2014-04-23' (length=10)

我在Twitter Bootsrap 3中添加了Tab.js作为我的标签。然后我使用这些数组在3个foreach循环中显示计划:

echo '<div class="tab-content">';
$a = 1;
$class_active = 'tab-pane fade in active';

  foreach ($dates_unique as $dates) {

    echo '<div id="section' . $a . '" class="' . $class_active . '">';
    $a++;
    $class_active = "tab-pane fade";

      foreach ($overlap_array as $row) {

        if ($dates == $row[0][0]) {
        echo '<div class="rows">';

          foreach ($row as $event) {

          $left = ((($event[3] - 36000) / 468)) + 0.1;
          echo '<a href="event/' . $event[6] . '"><div style="width:' .
          (($event[4] / 468) - 0.4) .'%; left: ' . $left . '%;" class="collumn">'.
          $event[5] . ' ' . $event[1] . ' - ' . $event[2] .' '. $event[0] .
          '</div></a>';

          }
          echo '</div>';
        }
      }
      echo '</div>';
  }
  echo '</div>';

如何在这里减少foreach循环的数量,或者我应该用另一种方式来显示时间表?

0 个答案:

没有答案