Entry
How can I show rows from a database in a table with alternating colors?
How can I use the Modulus Operator in PHP?
In a search result, how do you change the table (with the results) row color every result?
May 14th, 2002 20:37
Alder Rus, Jens Clasen, Amit Arora, Philip Olson, Robert Sherman, Wedgx, Nathan Wallace,
If it's simply in a loop, this may suit your needs:
$bgcolor = ($i++ & 1) ? '#ffffff' : '#000000';
Which will alternate between the two. You should first initialize $i
outside the loop as $i = 0; Also, look into "Modulus" as the following
also works (a little less efficient but who's counting):
$bgcolor = ($i++ % 2) ? '#ffffff' : '#000000';
Now let's say you wanted to alternate between four colors, you could do
something like:
$colors = array('black','green','blue','yellow');
And within the loop:
$bgcolor = $colors[$i++ % 4];
Which makes modulas more flexible then bitwise (the &) for this sort of
thing. One thing to remember is % is the modulus operator, it tells you
the remainder after the two numbers are divided. I hope that makes
sense and if not, search google ... it's not PHP specific. Here's a
simple example:
$i = 0;
while($row = mysql_fetch_array($result)) {
$bgcolor = ($i++ & 1) ? '#ffffff' : '#000000';
print "<tr bgcolor='$bgcolor'>\n";
print "\t<td>". $row['name'] ."</td>\n</tr>\n";
}
Spend some time on it as it'll eventually make sense. For kicks,
here's yet another example:
$colors = array('black','green','blue','yellow');
$num_colors = count($colors);
$i = 0;
for ($a=0; $a <= 20; $a++) {
$bgcolor = $colors[$i++ % $num_colors];
print "$bgcolor<br>\n";
}
Granted we could have just used $a instead of $i but again, do as you
wish. If the use of ? and : looks funny to you, this is called a
ternary conditional operator, it's briefly mentioned here:
http://www.php.net/manual/language.expressions.php
http://www.cs.bilkent.edu.tr/~will/courses/CS101/slides03/tsld036.htm
So essentially, the following provide identical results in PHP:
if ($fruit == 'banana') {
$myfav = 'my favorite fruit!';
} else {
$myfav = 'not a banana';
}
$myfav = ($fruit == 'banana') ? 'my favorite fruit!' : 'not a banana';
Th bitwise operator we used above (the &) can be read about here :
http://www.php.net/manual/language.operators.bitwise.php
http://lal.cs.byu.edu/cs130/Lectures/lect24.html
The modulus remainder operator (the %) can be read about here:
http://www.php.net/manual/language.operators.php
To explain modulus, it returns the remainder. For example:
0 % 2 // 0 // false
1 % 2 // 1 // true
2 % 2 // 0 // 2 goes into 2 once, none remain // false
3 % 2 // 1 // 2 goes into 3 once, then 1 remains // true
4 % 2 // 0 // 2 goes into 4 twice, none remain // false
When something is 0, it is considered false so the above statements
prove false which then assigns the "false color." Next time around 1
is returned which evaluates to true. The "true color" is then chosen.
This continues on and on throughout the loop. The number goes from 0
to n, as we increment it (in the above example we increment $i by 1
every time)
if (true) {
'do something';
} else {
'do something else'
}
Now for example, to use our modulus knowledge, let's return false every
third time, as to evalate true twice for every false:
0 % 3 // 0 // false
1 % 3 // 1 // true
2 % 3 // 2 // true
3 % 3 // 0 // false
4 % 3 // 1 // true
5 % 3 // 2 // true
6 % 3 // 0 // false
7 % 3 // 1 // true
8 % 3 // 2 // true
9 % 3 // 0 // false
See how that works? 0 evaluates to false, all other numbers evaluate
as true and from that, our bidding is done.
And lastly, read about incrementing here:
http://www.php.net/manual/language.operators.increment.php
With all this knowledge, you should be able to create all kinds of
different alternating colored rows :-)