CSS - background color of table row odd/even

后端 未结 9 961

I have a table that is dynamically generated by PHP. I am hoping that I can use CSS to apply a background color based on where the table row is odd/even, i.e. the backgroun

相关标签:
9条回答
  • 2020-12-10 08:39

    i guess the best option is to use the internal iterator of foreach :

    example :

    <table>
    <?php foreach ($foo as $key => &$bar) : ?>
    <tr class="<?php echo ($key + 1) % 2 ? 'odd' : 'even'; ?>">
    <td> ... </td>
    </tr>
    <?php endforeach; ?>
    </table>
    
    0 讨论(0)
  • 2020-12-10 08:42

    this is My way, you can try this

    <style type="text/css">
    .even{
     background-color:#333;
    }
    .odd{
     background-color:#666;
    }
    </style>
    
    <?php
    $i=0;
    foreach( ... ){ 
    ++$i;
    ?>
    <tr class="<?php echo ($i%2) ? 'even' : 'odd' ?>">
     <td>..</td>
    </tr>
    <?php } ?>
    

    ...

    0 讨论(0)
  • 2020-12-10 08:50

    You can do something like this:

    $counter = 0;
    while(............){
      $counter++;
    
      $bgcolor = ($counter % 2 === 0) ? 'red' : 'blue';
    
    }
    

    Now you can use bgcolor="<?php echo $bgcolor;?>" for your TDs :)

    Note that above will work in all browsers including IE6.

    0 讨论(0)
提交回复
热议问题