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.





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.
@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.
Have you ever used jquery? With it, you can do:
$(‘tr:odd td’).addClass(‘alt’)
Can’t get any simpler than that.
@Austin: Word. I love jQuery.
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”;
Your code stripped out my html :P
@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" : "";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/
@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.
And I’ve finally understood the snappy little trick for alternating comment boxes. Excellento. Thanks!
Still alive buddy?
That’s very clever. I’ve seen this “problem” tackled in many ways, but that’s a new one to me. Nice! :)
I did it like this, a bit longer:
http://www.dio5.com/blog/2007/03/06/Zebra_tables_with_PHP
see it started at the park,used to chill after dar. Martino Heino.
when they say it’s ove. Mihangel Caiaphas.
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.