SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Apr 15, 2002, 18:14 #1
TOGGLE... alternating row colours, HOW??
Ok I'm pulling some rows from MySql, and I'm putting each row in a HTML table row. I basically want to alternate the rows' background colour, so row one will be for ex. blue, row two green, row three blue again, and so on...
I've set up variables for all the bgcolor tags, but what I can't figure out is how to toggle these variables between two value sets every time my while loop runs.
What I tried is:
Code:if($toggle=1): $toggle=2; $dkcolour = "#669900"; $ltcolour = "#99FF00"; $hr = $hrclass[1]; elseif($toggle=2): $toggle=1; $dkcolour = "#006666"; $ltcolour = "#0099FF"; $hr = $hrclass[1]; endif;
Thx!
-
Apr 15, 2002, 18:56 #2
- Join Date
- Feb 2002
- Location
- CA, USA
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In your if() statements, you need to change "=" to "==". Otherwise, you're always setting $toggle to either 1 or 2, which always evaluates to TRUE. (You are using PHP, right?)
You can also use the modulus operator to test whether your toggle variable is even or odd:
PHP Code:$i = 0;
while(...)
{
...
$dkcolour = ($i%2) ? "#669900" : "#006666";
$ltcolour = ($i%2) ? "#99ff00" : "#0099ff";
$i++;
...
}
Jon Sagara
Y.A.P.P.
-
Apr 18, 2002, 09:31 #3
Great, thx alot, changing to == did the trick. Is there any reason why I would want to do it the other way? I mean, is it a better way to code it or does it amount to the same thing?
-
Apr 18, 2002, 10:40 #4
- Join Date
- Feb 2002
- Location
- CA, USA
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Fewer instructions. The end result is the same.
Jon Sagara
Y.A.P.P.
-
Apr 19, 2002, 04:08 #5
- Join Date
- Dec 2000
- Location
- The flat edge of the world
- Posts
- 838
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Using the ternary operator method also speeds up the script, but the reason I use it simply 'cos it's one line.
Work smarter, not harder. -Scrooge McDuck
-
Apr 19, 2002, 08:48 #6
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
* looks in JS forum ... looks here ... *
Similarities i see!
There was a large post in JS (top of the forum)
which harped on about this kind of thing... maybe
the two overlap a little
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
-
Apr 22, 2002, 05:46 #7
Would someone be so kind as to explain the actual funciton of the 'modulus' operator. I want to incorporate this method in my code but i hate to write code i don't actually understand!
Thx!
-
Apr 22, 2002, 09:11 #8
- Join Date
- Feb 2002
- Location
- CA, USA
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The modulus operator (%) returns the remainder of a division operation.
For example, 5/2 = 2 remainder 1, right? % returns the 1 of that division operation.Jon Sagara
Y.A.P.P.
-
Apr 22, 2002, 12:23 #9
- Join Date
- Feb 2002
- Location
- CA, USA
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Forgot this part:
In our case, dividing any number by two and looking at the remainder will tell you whether or not that number is odd or even:
1%2 = 1
2%2 = 0
3%2 = 1
4%2 = 0
5%2 = 1
...
and on and on and on...Jon Sagara
Y.A.P.P.
Bookmarks