[英]Dynamic Table Continuous <td> Count
我陷入一個奇怪的問題,我想顯示連續的數字,直到行和列的最后一行,而不是顯示從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>";
?>
謝謝
$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>";
您正在輸出$ td,它將在每個新的表行處重置。 相反,如果我沒有記錯的話,您想輸出一個遞增的$ 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.