#2: Highlighting Alternate Row Colors in a Table

When you have lots of information presented in rows, such as topics in a forum or line items in a shopping cart, individual rows are much easier to read when every other row is a slightly different color. The first step is to define colors for table rows in your style sheet.

tr.row1 {
    background-color: gray;
}

tr.row2 {
    background-color: white; 
}

There are two style classes here for table row tags (<tr>), row1 and row2. As with any CSS, you can place this inside a preexisting CSS file that your document includes or enclose it within <style> </style> tags in your document head.

Now let's define a function that alternately returns these classes. There's a little trick here—you pass an integer variable ...

Get Wicked Cool PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.