How to create a table with alternative row style using PHP?

Table With Alternative Row Style

Figure 1: Table with Alternative Row Style

How to create a table with look and feel similar to Figure 1 from above? Follow these steps below:

 

Step 1: Create CSS style for the even an and odd class. In figure 2, the background color of the even and odd table are set to different variation of grey color.

CSS Odd Even Class

Figure 2: CSS Code for Odd and Even class

Step 2: Display array data in a table and set table row class to odd or even. Figure 3 displays the code for display the table from Figure 1.

PHP Code To Display Table with Alternative Row Styles

The array $tblData contains randomly generated integers. The variable $key is the array index and $value is the actual integers.

The key line of code to differ the odd and even row is:

The Key Line of Code for Differentiate Odd and Even Rows

($key & 1)  checks if the array index is even or odd. The code sets the table row style to even and odd according the array index.

Did you like this? Share it: