Title:Alternating row colors tutorialContributor: Vlad B. (aka glipy)
Last Update: Wednesday June 13 12:46 EDT 2000
Sometimes, when we work with tables, we probably want to change the color for every row (<td>). There are probably many ways to do this, but I will describe the most simple of it - %.
So... let's say we have an array with 26 elements. But we don't really know that, so we do a `$count = count($myarray);`. Good! Now we know how many elements the array has, so let's get to the point.
We want to print every element on a line, in a row. So ... we will do something like the following:
gee
<?
// hmm... let's say the array exists. as I said, it has 26 elements.
$array = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "26");
$count = count($array);
for($x=0;$x<=$count;$x++) { ?>
<tr><td><? echo $array[$x]; ?></td></tr>
<? } ?>
Eample:
gee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
26
Now let's make the code look nice ;)
gee
<?
// hmm... let's say the array exists. as i said, it has 26 elements.
$count = count($array);
for($x=0;$x<=$count;$x++) {
if(($x % 2) == 0) { $c = "#9999CC"; }
else { $c = "#CCCCCC"; }
?>
<tr><td bgcolor="<?= $c ?>">
<?= $array[$x]; ?>
</td></tr>
<? } ?>
Example:
gee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
26
There you go.. I hope there are no code mistakes in my code. Happy coding!
Anyone who wishes to make additions or changes to this
PHP Tip email them to webmaster@linuxguruz.org
This document is Copyright (c) 1999 by LinuxGuruz