8.8.1. ProblemYou want to display a table of information with alternating rows having different visual appearance. For example, you want to have even-numbered rows in a table have a white background and odd-numbered rows have a gray background. 8.8.2. SolutionSwitch back and forth between two CSS styles as you generate the HTML for the table. Example 8-16 uses this technique with data retrieved from a database. Generating an HTML table with alternating row styles
8.8.3. DiscussionThe key to the concise code in Example 8-16 is the array of CSS class names in $styles and the use of %, PHP's "remainder" operator. The remainder operator returns the remainder after dividing two numbers. The remainder when dividing something by two (in this case, the row number in the result set'$i) is either 0 or 1. This provides a handy way to alternate between the first and second elements of the $styles array. 8.8.4. See AlsoDocumentation on PHP's arithmetic operators at http://www.php.net/language.operators.arithmetic. |