如何在PHP中为偶数/奇数表行着色

and*_*dy 1 php html-table

好的,我有一个问题,我有这个代码,我将在下面列出.我需要使偶数行为浅蓝色,奇数为白色.现在它没有出现所以我假设我做错了.现在我需要在其中执行命令,以便行看起来像我需要它的方式吗?

<html>
<head>

<title> Html Tables</title>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
  if ($rowcount%2==0){
   echo "</tr>";
   echo "<tr bgcolor=\"#5CCDC9\">\n";
   }
   else{
    echo "</tr>";
    echo "<tr bgcolor=\"#FFFFFF\">\n";
    }
    $rowscount++;
   }
 }
echo "</tr>";
echo "</table>";
 ?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

好吧,我在阅读一些事情后想要更好地理解这一点,这是我的新代码

<html>
<head>

<title> Html Tables</title>
<style type=<\"text/css\">
.even { bgcolor:#5CCDC9; }
.odd { bgcolor:#FFFFFF; }
</style>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
 if ($rowcount%2==0){
    echo "</tr>";
   echo "<tr class=\"even\">\n";
   }
   else{
    echo "</tr>";
    echo "<tr class=\"odd\">\n";
    }
    $rowcount++;
   }
 }
 echo "</tr>";
 echo "</table>";
 ?>
 </body>
 </html>
Run Code Online (Sandbox Code Playgroud)

现在我只是不明白如何用PHP编写它我正在阅读并试图弄清楚如何理解它.对不起,我是新手.

And*_*oso 5

您正在使用$rowcount条件和初始化,但$rowscount在增量中使用(在"s"中).

注意:你应该真正使用CSS而不是bgcolor属性.