PHP 作业2.6 九九乘法表

366 阅读1分钟

要求


使用php打出九九乘法表
使表格奇偶数行不同颜色

\

效果图


在这里插入图片描述

\

index.php代码


第一种

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">

  <title>九九乘法表</title>
  <style>
    body {
      background-color: #dedede;
      font-family: "楷体";
    }

    .main {
      border: 7px dotted #00bcff;
      width: 1200px;
      height: 500px;
      margin: 222px auto;
      font-size: 28px;
    }

    h1 {
      text-align: center;
    }
  </style>
</head>

<body>
  <div class="main">
    <h1>九九乘法表</h1>
    <?php

    echo "<table  style='margin:40px'>";
    for ($row = 1; $row <= 9; $row++) {
      if ($row % 2 == 0) {
        for ($col = 1; $col <= $row; $col++) {
          $sum = $col * $row;
          echo "<td style='color:#EA384E'>{$col}x{$row}=" .  $sum . "&nbsp;&nbsp;&nbsp;</td>";
        };
      } else {
        for ($col = 1; $col <= $row; $col++) {
          $sum = $col * $row;
          echo "<td style='color:#44B556'>{$col}x{$row}=" .  $sum . " </td>";
        };
      }
      echo "<tr>";
    }
    echo "</table>";
    ?>

  </div>
</body>

</html>

\

第二种

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">

  <title>九九乘法表</title>
  <style>
    body {
      background-color: #dedede;
      font-family: "楷体";
    }

    .main {
      border: 7px dotted #00bcff;
      width: 1200px;
      height: 500px;
      margin: 222px auto;
      font-size: 28px;
    }

    h1 {
      text-align: center;
    }

    .odd {
      color: #EA384E
    }

    .even {
      color: #44B556
    }
  </style>
</head>

<body>
  <div class="main">
    <h1>九九乘法表</h1>
    <?php

    echo "<table  style='margin:40px'>";
    for ($row = 1; $row <= 9; $row++) {
      if ($row % 2 == 0) {
        for ($col = 1; $col <= $row; $col++) {
          $sum = $col * $row;
          echo "<td class='odd'>{$col}x{$row}=" .  $sum . "&nbsp;&nbsp;&nbsp;</td>";
        };
      } else {
        for ($col = 1; $col <= $row; $col++) {
          $sum = $col * $row;
          echo "<td class='even'>{$col}x{$row}=" .  $sum . " </td>";
        };
      }
      echo "<tr>";
    }
    echo "</table>";
    ?>

  </div>
</body>

</html>

\

第三种

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">

  <title>九九乘法表</title>
  <style>
    body {
      background-color: #dedede;
      font-family: "楷体";
    }

    .main {
      border: 7px dotted #00bcff;
      width: 1200px;
      height: 500px;
      margin: 222px auto;
      font-size: 28px;
    }

    h1 {
      text-align: center;
    }

    tr:nth-of-type(odd) {
      color: #EA384E
    }

    tr:nth-of-type(even) {
      color: #44B556
    }
  </style>
</head>

<body>
  <div class="main">
    <h1>九九乘法表</h1>
    <?php

    echo "<table  style='margin:40px'>";
    for ($row = 1; $row <= 9; $row++) {
      for ($col = 1; $col <= $row; $col++) {
        $sum = $col * $row;
        echo "<td >{$col}x{$row}=" .  $sum . "&nbsp;&nbsp;&nbsp;</td>";
      };
      echo "<tr>";
    }
    echo "</table>";
    ?>

  </div>
</body>

</html>