Use PHP to make alternate coloured rows
I came across an article that described how to use PHP to make alternate coloured rows in a table and was surprised at how difficult they made it. There is a very simple way to display alternate coloured rows in tabular data. Read on for more info.
Use the following PHP code to display alternate rows in different colours. What this is actually doing is setting the class for one row to: odd, and the next row to: even. Then in my css stylesheet I will define .odd and .even to have different background colours. I see this as a very clean and manageable way to do this common task.
<?php $i = 0; //Set the index to zero //Put your code here to loop through the database result or whatever it is you are displaying //Then display the table row ?> <tr class="<?php if($i%2){ echo 'odd'; } else { echo 'even'; } ?>">
This same idea can be used in probably all web development coding languages with slightly different syntax.