Output zebra stripe rows in PHP


/ Published in: PHP
Save to your folder(s)

This is a simple way to stripe alternate rows of output or to do anything different as I process each alternate row. Typically, I set the element's class to "myEvenRow" or "myOddRow" then output the data just after the IF statement.

I found this full example at nullislove.com. The page also has a Ruby example. Full props to them.


Copy this code and paste it in your HTML
  1. <?php // PHP version
  2. $i = 0;
  3. // have_rows_to_output is a stand-in for your loop's code
  4. while (have_rows_to_output) {
  5. if (fmod($i, 2) == 0) { // it's an even number
  6. output_even_style_row;
  7. } else { // it's an odd number
  8. output_odd_style_row;
  9. }
  10. $i++;
  11. }
  12. ?>

URL: http://www.nullislove.com/2007/05/09/zebra-striping/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.