我遇到了一个奇怪的问题,我想显示连续数字,直到行和列中的最后一个, 而不是它显示形式1-50然后再次行以1到50开始。
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>$td</td>";
}
echo "</tr>";
}
echo "</table>";
?>
由于
答案 0 :(得分:1)
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
$style = "green";
if ($tr % 2 == 0) {
$style = "#ccc";
}
echo "<tr style='background-color:".$style."'>";
for($td=1;$td<=$cols;$td++)
{
echo "<td>$x</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
答案 1 :(得分:0)
您正在输出$ td,它会在每个新的tablerow重置。 相反,如果我没有弄错的话,你想输出一个递增的$ x。
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>" . $x . "</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
?>
回应你对另一个答案的评论:如果你想要交替的行颜色,你可以使用$ tr并检查它是均匀的还是不均匀的:
if($tr % 2 == 0)
{
// use color1
}
else
{
// use color2
}