SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Thread: 2,4,6,8 or 1,3,5,7
-
Jun 20, 2001, 07:10 #1
- Join Date
- Nov 2000
- Location
- Oslo, Norway
- Posts
- 413
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
2,4,6,8 or 1,3,5,7
Hi guys,
I want to print out a table consisting on an unknown amount of rows (based on a select query) and I want every second row to have the same colour. I was thinking that it would be a good idea to have an $i adding one for each result from the SELECT query, and then set the colour based on whether the $i was 2,4,6,8,10 or 1,3,5,7,9. But how can I determine this without having to write a huge if block?
Thanks in advance !
-
Jun 20, 2001, 07:17 #2
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:$color = ($i % 2 == 0) ? "#FFFFFF" : "#E9E9E9";
-
Jun 20, 2001, 07:24 #3
- Join Date
- Jun 2001
- Location
- Newcastle, England
- Posts
- 268
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
I assume you have some sort of loop wrich goes through your results and writes them out? Well since what you really want to do is toggle between 2 colours, instead of using a variable which is incremented each record try using one that just swaps between two values.
Say you called the variable $oddrow. Initialise it to true. Then inside your loop you can do a simple:
PHP Code:if ($oddrow) {
$colour = "#000000";
$oddrow = false;
}
else {
$colour = "#FFFFFF";
$oddrow = true;
}
You could rewrite that code to get rid of the $oddrow variable like so:
PHP Code:if ($colour = "#000000") {
$colour = "#FFFFFF";
}
else {
$colour = "#000000";
}
Hope that helps.
[Edit: or just do that ^]
-
Jun 20, 2001, 08:08 #4
- Join Date
- Nov 2000
- Location
- Oslo, Norway
- Posts
- 413
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks to you both. I went with Chris' suggestion, and it worked like a charm.
-
Jun 20, 2001, 11:03 #5
-
Jun 20, 2001, 11:05 #6
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Search for information on the "ternary operator" -- either on PHP.net, or even on these forums. I, and several other members, have written posts describing the ternary operator -- I love it.
-
Jun 20, 2001, 11:09 #7
- Join Date
- Apr 2000
- Location
- Melbourne, Australia
- Posts
- 2,571
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
TWT's method uses two somewhat 'exotic' (depending on your background) features that are available in most C-style languages:
The modulus operator (%), which performs an integer division and produces the remainder as a result. Thus, $integer % 2 will always equal 0 if $integer is even, and 1 if $integer is odd.
The ternary operator (condition ? truevalue : falsevalue), which produces one of two values (truevalue or falsevalue), depending whether conditon is true or false.
Thus, the command
$color = ($i % 2 == 0) ? "#FFFFFF" : "#E9E9E9";
Assigns the variable $color one of two values: "#FFFFFF" if the condition is true, or "#E9E9E9" if the condition is false. The condition in question is ($i % 2 == 0), or "$i is even".
Make sense?
Both of these operators are documented in the PHP manual.Kevin Yank
CTO, sitepoint.com
I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
Baby’s got back—a hard back, that is: The Ultimate CSS Reference
-
Jun 20, 2001, 11:22 #8
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well put Kevin -- yeah, I guess it is sort of exotic. I love 'em both, though. I love the fact that I can alternate colors and such with that one line of code. It's complex, yet elegant. The ternary operator is easily my favorite little "trick."
-
Jun 20, 2001, 13:08 #9
- Join Date
- May 2001
- Location
- lalal
- Posts
- 85
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
to be a bit quicker ..
PHP Code:$colour=(++$i&1)?'red':'blue';
-
Jun 20, 2001, 13:31 #10
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ah, the reference operator. I've heard of it. Could you explain it to me?
-
Jun 20, 2001, 15:48 #11
- Join Date
- Apr 2000
- Location
- Melbourne, Australia
- Posts
- 2,571
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
That's no reference -- that's a bitwise AND.
Any odd number will have its lowest bit (in the binary representation of the number) set to 1, while any even number will have it set to 0.
ANDing 1 with a number will always produce 1 if the lowest bit of the number is set to 1 (i.e. it is odd) and 0 if the lowest bit of the number is set to 0 (i.e. it is even). This can then be used as the condition of the ternary operator as above.Kevin Yank
CTO, sitepoint.com
I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
Baby’s got back—a hard back, that is: The Ultimate CSS Reference
-
Jun 20, 2001, 16:50 #12
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Are you sure? I read that it was called the "reference operator" on PHP.net. Is it possible it could be called that without actually referring to a reference?
-
Jun 20, 2001, 16:56 #13
- Join Date
- Apr 2000
- Location
- Melbourne, Australia
- Posts
- 2,571
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
It's the reference operator as well. Just depends on the context. The '-', for example, can be the unary operator (makes a single value negative) or the standard subtraction operator.
In this context, & is a bitwise AND.Kevin Yank
CTO, sitepoint.com
I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
Baby’s got back—a hard back, that is: The Ultimate CSS Reference
-
Jun 20, 2001, 16:57 #14
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I see. Makes sense now. Thank you.
Bookmarks