2011-05-15 116 views
0

我有一個計數數組的數組。我想顯示他們在一個特定列數的表格或阻止列表中,例如6使用php的foreachecho函數,以便我可以在一個循環中。我怎樣才能使用csshtml在塊列表/表格中顯示數組元素

下面是我講的

+-----+-----+-----+-----+-----+-----+ 
| 1 | 2 | 3 | 4 | 5 | 6 | 
+-----+-----+-----+-----+-----+-----+ 
| 7 | 8 | 9 | 10 | 11 | 12 | 
+-----+-----+-----+-----+-----+-----+ 
| 13 | 14 | 15 | 16 | 17 | 18 | 
+-----+-----+-----+-----+-----+-----+ 
| 19 | 20 | 21 | 22 | 23 | 24 | 
+-----+-----+-----+-----+-----+-----+ 

如果通過設置<table>

+0

所以你在找什麼樣的解決方案目前? PHP和一個表或列表和CSS? – Gumbo 2011-05-15 17:16:29

+0

是的,'

'是這裏的正確選擇。你有沒有試過編寫任何代碼? – 2011-05-15 17:17:03

+0

hi @Gumbo,我有一個'

'和'
'..我想填寫裏面,我的意思是,我想用php打印/回顯''和'​​'。我不知道如何做到這一點...... :( – 2011-05-15 17:20:02

回答

3
$elements = array_chunk($elements, 6); 
$html = '<table>'; 
foreach($elements as $tr){ 
    $html .= '<tr>'; 
    foreach($tr as $td) $html .= '<td>'.$td.'</td>'; 
    $html .= '</tr>'; 
} 
$html .= '</table>; 
+0

非常乾淨,我喜歡它 – Ascherer 2011-05-15 17:20:53

+0

回聲會在這裏的最後一行之後 'echo $ table;' – Ascherer 2011-05-15 17:35:19

+0

@Asherehere,我對所有'$ html'行和'$ table'進行了迴應..它可以工作... – 2011-05-15 17:38:51

0

嘗試做這將是更好的這

<table> 
    <tr> 
     <?php 
     $columns = 6; 
     $i = 0; 
     foreach($array as $item) 
     { 
      echo "<td>$item<td>"; 
      if($i++ >= $columns) 
      { 
       $i = 0; 
       echo "</tr><tr>"; 
      } 
     } 
     ?> 
    </tr> 
</table> 
+0

這樣做是行不通的嗎? – Ascherer 2011-05-15 17:32:04