2012-12-19 46 views
2

我有一個從複雜的循環填充表的問題。複雜的多維循環到表

該表由7天的日曆表組成,每天應包含其下的日事件。

Array 
(
    [2012-12-16] => Array 
     (
     ) 

    [2012-12-17] => Array 
     (
      [0] => Array 
       (
        [date] => 2012-12-17 
        [time_booked] => 00:09:00 
        [time_start] => 00:00:00 
        [time_end] => 00:00:00 
        [name] => maha mostafa elrashed 
        [first_name] => marwan 
        [title] => root cannal 
       ) 

      [1] => Array 
       (
        [date] => 2012-12-17 
        [time_booked] => 09:00:00 
        [time_start] => 00:00:00 
        [time_end] => 00:00:00 
        [name] => demo demo eldemo 
        [first_name] => marwan 
        [title] => ultrasound 
       ) 

     ) 

    [2012-12-18] => Array 
     (
      [0] => Array 
       (
        [date] => 2012-12-18 
        [time_booked] => 09:00:00 
        [time_start] => 00:00:00 
        [time_end] => 00:00:00 
        [name] => demo demo eldemo 
        [first_name] => marwan 
        [title] => root cannal 
       ) 

     ) 

    [2012-12-19] => Array 
     (
     ) 

    [2012-12-20] => Array 
     (
     ) 

    [2012-12-21] => Array 
     (
     ) 

    [2012-12-22] => Array 
     (
     ) 

) 

這個數組的例子我想轉換成表格。目前我發現最簡單的方法是將每天的打印爲ul並將它們留在,但我真正想要的是將它們製作成表格,以便將來可以使用jQuery對該表格進行排序。

foreach($ztable as $t){ 
    echo"\n<ul style='float:left;width:140px'>"; 
    foreach($t as $r){ 
     echo "<li class='{$r['first_name']}'>{$r['name']}</li>"; 
    } 
     echo "</ul>\n"; 
} 

有什麼建議嗎? 我使用笨2的方式

編輯:

top table is from the suggested answer, the lower one is how I want it to be

頂級表從建議的答覆,下一個就是我希望它是。

+0

看看CI自動生成表格:http://ellislab.com/codeigniter/user-guide/libraries/table.html – cartalot

回答

2

你的問題非常模糊。你說你想在創建一個<ul>的同時擁有工作代碼,而你實際上想要創建一個表。嗯。

這裏是你如何能做到一個表:

// list of fields that will be rows 
$fields = array(
    'date' => 'Date', 
    'time_booked' => 'Time of booking', 
    'time_start' => 'Start time', 
    'time_end' => 'End time', 
    'name' => 'Name', 
    'first_name' => 'First name', 
    'title' => 'Title' 
    ); 

// echo table header 
echo '<table border="1"><tr><th>Day</th>'; 
foreach ($fields as $field) 
{ 
    echo '<th>' . $field . '</th>'; 
} 
echo '</tr>'; 

// echo table rows 
foreach($ztable as $day => $data) 
{ 
    echo '<tr><td>' . $day . '</td>'; 
    // if some field is not present for current day, we show '-' 
    foreach ($fields as $field_name => $field) 
    { 
     echo '<td>' . (isset($data[$field_name]) ? $data[$field_name] : '-') . '</td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
+0

我不認爲這會完全工作;需要通過'$ data'循環,因爲它將是一個數組。 '\t如果(!空($數據)){ \t \t的foreach($數據爲k $ => $ v)的{ \t \t \t回波$ V [$ FIELD_NAME] 「
」。 \t \t} \t} else { \t \t echo「 - 」; \t}' – stormdrain

+0

@stormdrain根據數組提供的判斷,他可能沒有數據,有些日子我會假設有一些不完整的數據集。這就是爲什麼通過'$ data'循環可能會破壞表格佈局,而循環通過預定義的字段集合始終會產生相同的佈局,數據或沒有數據的' - '。 – Ranty

+0

在我評論的循環中,它檢查數組是否爲空,如果它是在那裏放置一個「 - 」:'if(!empty($ data))' – stormdrain

0

我覺得你的陣列格式不正確。請嘗試這種格式。

Array('2012-12-16' => Array(), 
      '2012-12-17' => Array 
       (
       '0' => Array 
        (
        'date' => '2012-12-17', 
        'time_booked' => '00:09:00', 
        'time_start' => '00:00:00', 
        'time_end' => '00:00:00', 
        'name' => 'maha mostafa elrashed', 
        'first_name' => 'marwan', 
        'title' => 'root cannal' 
       ), 
       '1' => Array 
        (
        'date' => '2012-12-17', 
        'time_booked' => '09:00:00', 
        'time_start' => '00:00:00', 
        'time_end' => '00:00:00', 
        'name' => 'demo demo eldemo', 
        'first_name' => 'marwan', 
        'title' => 'ultrasound' 
       ) 
      ), 
      '2012-12-18' => Array 
       (
       '0' => Array 
        (
        'date' => '2012-12-18', 
        'time_booked' => '09:00:00', 
        'time_start' => '00:00:00', 
        'time_end' => '00:00:00', 
        'name' => 'demo demo eldemo', 
        'first_name' => 'marwan', 
        'title' => 'root cannal' 
       ) 
      ), 
      '2012 - 12 - 19' => Array 
      (
      ), 
      '2012 - 12 - 20' => Array 
      (
      ), 
      '2012 - 12 - 21' => Array 
      (
      ), 
      '2012 - 12 - 22' => Array 
      (
      ) 
     ); 
+0

我很確定你在OP中看到的是數組的轉儲,而不是它是如何設置的。 – stormdrain

+0

@stormdrain yp它的陣列轉儲 – Zalaboza

1

既然你添加你想要的圖片,我扔在一起的一些毛,看代碼,得到的結果:

//provided $a is the array 
//table header with dates 
echo "<table class='table table-bordered'>"; 
echo "<tr>"; 

foreach($a as $date => $data){ 
    echo "<th>$date</th>"; 
} 

echo "</tr>"; 

//table row for appointment dates 
echo "<tr>"; 

foreach($a as $k=>$v){ 

    //if the array value isn't empty, we have data 
    if(! empty($v)){ 

     echo "<td>"; 
     echo "<ul>"; 

     //loop through the array to get all the names 
     foreach($v as $k => $v){ 

      echo "<li>".$v['name']."</li>"; 

     } 
     echo "</ul>"; 
     echo "</td>"; 

    }else{ 
      //there was no data for the date; empty td 
     echo "<td></td>"; 

    } 

} 

echo "</tr></table>"; 

應該足以讓你在正確的軌道上。