This site requires Flash Player 9 or better.

Featured

Zebra tables

Last week I was checking out the code of an open source project that used 10 lines of code just to generate a “odd” / “even” string to use as the html class for a table row. This is how I do it in one line:

<?php 
for($i=0;$i<10;$i++) { 
    $row = (!isset($row) || $row==='even')?'odd':'even'; //line that generates the classname 
    echo $row; 
}

The only easier way that I’m aware of would be to use the codeigniter alternator.

 echo alternator('string one', 'string two');

Discussion

3 comments for “Zebra tables”

  1. Even shorter, using modulo :
    for ($i = 0; $i < 10; $i++) {
    echo ($i % 2 == 0 ? ‘even’ : ‘odd’);
    }

    Posted by Wim Godden | December 8, 2008, 4:09 am
  2. Even easier when using Smarty:

    :-)

    Posted by Bart Dens | February 17, 2009, 9:59 am
  3. @bart
    Might be, but I’m not a big fan of using a language (smarty) that gets compiled into another language.

    Posted by Andy Mathijs | February 19, 2009, 11:58 pm

Post a comment