在PHP表中显示ODD红色和EVEN绿色


Display ODD red and EVEN Green in PHP tables

我做了一个简单的表格数学计算器。

只是现在我想当你回显数字时,ODD数字是红色的,偶数显示绿色。css可以做到这一点吗?还是我必须重新构建整个代码?

代码HTML+PHP

      <html>
      <head>
      <link rel="stylesheet" type="text/css" href="style.css">
      </head>
      <body>
       <center>
       <form action = "pagina2.php" method = "post">
      <input name = "invoer" type = "text" value = "">
      <input name = "knop" type = "submit" value = "Verstuur">
      </form>
      </center>
      </body>
      </html>
//PHP
      <html>
      <head>
      <link rel="stylesheet" type="text/css" href="style.css">
      </head>
<body>
      <center>
      <?php
      if ($_SERVER['REQUEST_METHOD'] == "POST")

    {
        if (isset($_POST['invoer']))
    {
        if (is_numeric($_POST['invoer']))
    {
        for ($i = 1; $i <= 10; $i++)
    {
        echo $i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "<br />";
       }
    }
            else
       {
            echo "Vul een getal in!";
      }
  }
    else
  {
            echo "Niks ingevoerd!";
  }
 }
 ?>
 </center>
  <br>
   <center>
   <input action="action" type="button" value="Opnieuw" onclick="history.go(-1);" />
    </center>
    </body>
    </html>

问候。

更改此回波状态

echo $i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "<br />";

$x=$i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "<br />";
if($x % 2 ==0 )
{
    echo '<font color="green">'.$x.'</font>';
}
else  echo '<font color="red">'.$x.'</font>';

我会使用modulas来缩短代码

  $number % 2 == 0 #means even

然后使用像.oodd或.eeven这样的css类,并将其注入到模板中

我假设您指的是变量$I:

echo '<div style="color: '.($i%2==0 ? 'green' : 'red').">". $i . " x " . $_POST['invoer'] . " = " . ($i * $_POST['invoer']) . "</div><br />";