search

Home  >  Q&A  >  body text

php - 如何用for的双层循环来控制隔行变色的表格

初学php小白一枚,恳请热心的人儿帮忙解答一下如题(◐﹏◐)
单层循环如下:

效果图如下:

想问一下如何用for的双层循环得到上面一样的效果图?

滿天的星座滿天的星座2738 days ago931

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 13:02:06

    echo '<table border="1">';
    for($i = 1; $i < 10; $i++) {
        echo $i%2 === 0 ? '<tr bgcolor="#ccc">' : '<tr>';
        for($j = 1; $j < 5; $j++) {
            echo "<td>$i$j</td>";
        }
        echo '</tr>';
    }
    echo '</table>';

    In fact, writing this in php is a waste of resources. In actual projects, it is recommended to use the CSS3 pseudo-class nth-child to complete this function

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:02:06

    I suggest you take a look at how bootstrap handles the color change of each row. Take a look at its table part

    reply
    0
  • Cancelreply