How to set alternate row color for an iterated table in php?

前端 未结 3 1791
广开言路
广开言路 2021-01-21 23:51

I am using PHP and I am iterating a table with a result array ... I want to add row color and alternate row color to it.... How to do so? Any suggestion...



        
3条回答
  •  佛祖请我去吃肉
    2021-01-22 00:31

    Use modulo

     $row) { ?>
        ', ($key % 2) ? 'odd' : 'even'); ?>
        // ...
    

    Then you can define CSS classes with the names .odd and .even and given them the background-color you want the rows to alternate with.

    With modern browsers (read: not IE 8 or lower) you can also do it directly in CSS with the :nth-child pseudo class:

    tr:nth-child(even) { background-color: #FFF; }
    tr:nth-child(odd) { background-color: #EEE; }
    

提交回复
热议问题