| Subcribe via RSS

Alternating Row Color in PHP

June 25th, 2009 | Comments Off | Posted in CSS, PHP, Uncategorized

In ASP.NET setting the background color for alternating rows is very simply. Just update the property on the Gridview and whalah! Crayola just marked up your database output in pretty colors. In PHP, its not quite as simple, but not terribly complicated either. Basically, we are going to set the odd and even row background colors in our css stylesheet then increment our loop value as we cycle through the records and dynamically populate our class value on the tr for the given row.

Stylesheet

PHP Code

This is a JSON result set we are looping through. The key line to focus on here is row 9 in the PHP code. This appends the i value we’ve been incrementing via the loop to the tr class property. This gives us that clean alternating row look within minimal work.

Tags: , ,