Re: change color
| From: | David Otton | Date: | Tue, 27 May 2003 03:43:56 +0000 |
| Subject: | Re: change color | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-149047@lists.php.net to get a copy of this message | ||
On Mon, 26 May 2003 23:01:25 -0400, you wrote:
>This was a question I've been kicking around myself. But Evan's answer
>is a bit confusing to me, can someone clear it up? Evan, can you explain
>it a bit better? Maybe show how it is supposed to look in the overall code?
Breaking it down slightly, compared to the other replies you've received:
You start by labeling each row (1, 2, 3, 4, etc). Odd-numbered rows we
colour white, even-numbered rows we colour green.
We already have a convenient label for our rows (the variable $i in the
original example), we just have to decide when it's odd and when it's even.
Even numbers are divisible exactly by 2, odd numbers leave a remainder.
To find the remainder after a division, we use the mod (%) operator.
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
5 % 2 = 1
etc.
if ($i % 2 == 1) {
echo ("white");
} else {
echo ("green");
}
The ?: stuff is just short-hand for if/else.