我需要在PHP中做一些“for”循环,它将生成带有垂直编号单元格的表。以下是更好理解的图片:
答案 0 :(得分:0)
我相信你正在努力实现这样的目标
<?php
$x = 0;
echo "<table >";
echo "<tr>";
for($x=1; $x<13; $x++){
echo "<td>$x</td>";
if($x%4 == 0 ){
echo "<tr>"."\n";
}
}
echo "</tr></table>";
?>
好运!
答案 1 :(得分:0)
以下是您要查找的代码:
<?php
$col = 4; //number of desired columns
$row = 5; //number of desired rows
echo "<table>\n";
for($i = 1; $i<$row+1; $i++)
{
echo "<tr>\n";
for($c = 0; $c<$col; $c++)
{
echo "<td>".($i + ($c*$row))."</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n"
?>
答案 2 :(得分:0)
那样做:
<?php
echo "<table>";
echo "<tr>";
function x($y){
return ($y<=1)?array($y):array_merge(x($y-1), array($y));
}
function y($x,$z){
foreach (range($x, count(x($z))) as $i) {
yield $i;
}
}
function z($x,$y){
return array_map(function($w){
return '<td>'.(1+$w).'</td>';
}, (iterator_to_array(y($x,$y))));
}
for($i=0;$i<4;$i++){
echo $i>0?"</tr><tr>":"";
foreach(z(4*$i, 4*($i+1)-1) as $z) echo $z;
}
echo "</tr>";
echo "</table>";
?>