1

嘿,我使用嵌套的 for 循环制作了一个简单的 php 表......它会是这样的......

1   2   3   4   5
6   7   8   9   10
11  12  13  14  15

但问题是,我无法使用列内的循环打印此值..那么解决方案是什么???请

我的代码:

echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
    echo "<tr>";
    for($col=1;$col<=5;$col++)
    {
        echo "<td>";
        echo "MY PROBLEM HERE...I cant print column numbers \n";
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table> \n";
4

3 回答 3

3
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
    echo "<tr>";
    for($col=1;$col<=5;$col++)
    {
        echo "<td>";
        //echo "MY PROBLEM HERE...I cant print column numbers \n";
        echo $col + ($row - 1) * 5;
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table> \n";
于 2013-04-09T08:35:26.777 回答
1

这不是$col + $row * 5 必须的$row - 1

<?php
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
    echo "<tr>";
    for($col=1;$col<=5;$col++)
    {
        echo "<td>";
        echo $col + ($row-1) * 5;
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table> \n";

?>
于 2013-04-09T08:39:36.483 回答
1

要保存多个循环:

$rows = 3;
$cols = 5;

$table = '<table border="1">';

for($i=1;$i<=$rows;$i++){
    $table .= '<tr><td>'.implode('</td><td>', range($cols*$i-$cols+1,$cols*$i)).'</td></tr>';
}

$table .= '</table>';
echo $table;
于 2013-04-09T08:52:04.920 回答