2011-05-15 121 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