Ok. I have seen a myriad of techniques on creating the PHP code to pump out alternating row classes on tables, list elements, what have you. It’s usually some counter variable that you divide by 2 to see if there is an even result (MOD operator). But then you got the whole initialize the variable, increment the variable, test the variable scenario. Blegh.

I’ve also seen some crazy technique using bitwise & operators, which is hardly readable for common folk, but I do admit is probably blazing fast. Only issue there, once again, you are maintaining a counter variable and incrementing it. Blegh.

I do not claim for this method to be better. I simply claim that this is my method and I use it. Take it as you will. When in Rome…

First, lets set up a style

<style>
tr.alt td{
background-color:#D5E0E1;
}
</style>

Done…

Next, the PHP code intermingled with some HTML

<?php
for($i = 0; $i < 10; ++$i){
$row_class = empty($row_class)? "alt" : "";
echo("
<tr class=\"$row_class\">
<td>
data 1
</td>
<td>
data 2
</td>
<td>
data 3
</td>
</tr>");
}
?>

The key for the alternating row classes of course, lies in one line of PHP code:
$row_class = empty($row_class)? "alt" : "";

Basically, I’m using PHP shorthand to do an if statement and check if the variable $row_class is empty or a blank string (“”). If it is then I set it to “alt”. As you can imagine, next row, the same statement is ran. This time, it’s not empty, so it gets set to empty, thus alternating between the “alt” class and empty.

Done…Seacrest out.

Comments

16 Comments

  1. Jacob Harvey said 2246 days ago

    This is definitely a nifty little trick. But I think you should have used a while loop or maybe a foreach in the example since you already have a counter going. I normally would use the $i and mod (%) to do the exact same thing in this situation.

    Still, as I said, this is a perfect method for getting around not having a counter to begin with.

  2. Rogie said 2246 days ago

    @Jacob: Well, you answered your own wish. This solution of course will work in any looping structure. I just happened to use the for…loop. Of course it seems redundant in this case…with the whole $i counter goin’ on, but smart chaps like you get it.

  3. Austin Schneider said 2238 days ago

    Have you ever used jquery? With it, you can do:

    $(‘tr:odd td’).addClass(‘alt’)

    Can’t get any simpler than that.

  4. Rogie said 2236 days ago

    @Austin: Word. I love jQuery.

  5. Eddy said 2229 days ago

    JQuery is great, but I believe Rogie was trying to show how to do this in a PHP class and have the rows assigned their values, then having css apply the alternating color. Using jquery with this just adds another script. Why bother when you can just simply have 2 css rules: 1 for standard and 1 for the alt row?

    Rogie… use a foreach :P I’ve also used just a counter and modulus to figure out what the current row class should be. So i guess its just a matter of preference of course.

    if( $rowCount % 2 == 1 ) $rowClass = “alt row something”;
    else $rowClass = “standard”;
    $result = “Some Data”;

  6. Eddy said 2229 days ago

    Your code stripped out my html :P

  7. Rogie said 2229 days ago

    @Everyone: I think you guys are missing the beauty of this code. You can use a foreach, you can use a for…, you can have it in a while loop. Basically, all you need is one line, not three lines of if statements:

    $row_class = empty($row_class)? "alt" : "";

  8. anonymus said 2212 days ago

    Well there’s a faster solution for this problem.
    Try $row_class = $i&1?’alt’:”;
    It is also very nice – just one line.

    Jonathan Snook wrote about this before: http://snook.ca/archives/php/the_modulo_oper/

  9. Rogie said 2212 days ago

    @anonymous: Yeah, cool function as well, however in your case you’ll always HAVE to keep incrementing an $i variable. For for loops using $i, you are golden, for foreach loops over an object, its nice to not have to create another variable and increment it.

  10. Lara said 2191 days ago

    And I’ve finally understood the snappy little trick for alternating comment boxes. Excellento. Thanks!

  11. Ben McNelly said 2179 days ago

    Still alive buddy?

  12. Sean said 2155 days ago

    That’s very clever. I’ve seen this “problem” tackled in many ways, but that’s a new one to me. Nice! :)

  13. Dieter said 2108 days ago

    I did it like this, a bit longer:
    http://www.dio5.com/blog/2007/03/06/Zebra_tables_with_PHP

  14. Martino Heino said 2073 days ago

    see it started at the park,used to chill after dar. Martino Heino.

  15. Mihangel Caiaphas said 2041 days ago

    when they say it’s ove. Mihangel Caiaphas.

  16. Clemens Lang said 1805 days ago

    There are a couple of approaches to this problem, and if you really want to keep low on lines of code, you should do something like print($myAltRow = (empty($myAltRow) ? ‘odd’ : ”));
    Using the fact that a variable assignment returns the assigned value in PHP here.

Sorry, the comment form is closed at this time.