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
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>
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 } ?>
...
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.