Posted By

dizzley on 08/06/09


tables color stripes reports

Versions (?)

Who likes this?

5 people have marked this snippet as a favorite


Output zebra stripe rows in PHP

 / Published in: PHP


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 The page also has a Ruby example. Full props to them.

  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. ?>

Report this snippet  

You need to login to post a comment.