在块列表/表格中显示数组元素
问题描述:
我有一个计数数组的数组。我想显示他们在一个特定列数的表格或阻止列表中,例如6使用php的foreach
和echo
函数,以便我可以在一个循环中。我怎样才能使用css
或html
?在块列表/表格中显示数组元素
下面是我讲的
+-----+-----+-----+-----+-----+-----+
| 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>
$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>;
尝试做这将是更好的这
<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
答
$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>;
答
尝试做这将是更好的这
<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
所以你在找什么样的解决方案目前? PHP和一个表或列表和CSS? – Gumbo 2011-05-15 17:16:29
是的,'
hi @Gumbo,我有一个'
回答
非常干净,我喜欢它 – Ascherer 2011-05-15 17:20:53
回声会在这里的最后一行之后 'echo $ table;' – Ascherer 2011-05-15 17:35:19
@Asherehere,我对所有'$ html'行和'$ table'进行了回应..它可以工作... – 2011-05-15 17:38:51
尝试做这将是更好的这
这样做是行不通的吗? – Ascherer 2011-05-15 17:32:04
相关问题