mau*_*lla 0 php jquery html-table
我正在努力抓住以下几点.我想基于PHP数组为每行表构建一个3数据单元.换句话说,如果数组中有3个值,则应该有如下结构:
<?php
$arr = array("value1","value2","value3");
?>
// Expected outcome:
<table>
<tr>
<td>value1</td>
<td>value2</td>
<td>value3</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
但是如果要将第4个值添加到数组中,它必须动态创建另一行,换句话说:
<?php
$arr = array("value1","value2","value3","value4");
?>
// Expected outcome:
<table>
<tr>
<td>value1</td>
<td>value2</td>
<td>value3</td>
</tr>
<tr>
<td>value4</td>
<td></td>
<td></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我真的不介意哪个解决方案,甚至是php和jQuery之间的混合,但只是我可以用来实现上述的东西.
使用模数.像这样:
<table>
<tr>
<?php
$i = 1;
foreach ($arr as $val){
$i++;
print '<td>'.$i.'</td>';
if ($i % 3 == 0){
print '</tr><tr>'^;
}
}
?>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
你需要为正确的html输出添加一些东西,但是"硬"部分已完成.
不要只是复制和粘贴,我没有测试代码,这是丑陋的.
| 归档时间: |
|
| 查看次数: |
6666 次 |
| 最近记录: |