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.
This is great.
I think a lot of people get sucked into trying to over-use the power that php has and end up overlooking something as simple as just setting classes to manipulate variations in visual appearance.
CSS is a powerful tool in itself and can be just as handy as a php snippet at times.
Thanks.