Alternating Colors For Table Rows
Moderator: General Moderators
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Alternating Colors For Table Rows
Hello,
I was wondering how I can get alternating background colors for my table rows, that come from a database.
Thanks
I was wondering how I can get alternating background colors for my table rows, that come from a database.
Thanks
Code: Select all
echo "<tr style=\"background-color: #".(($rowCounter++%2==0)?"ffffff":"000000").";\"><td></td></tr>";- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Code: Select all
echo '<tr'.((@$i^=1)?' class="invert"':null).'>';- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Here's a tutorial....Everah wrote:Isn't there a tutorial on this on our boards?
% is the modulus symbol. If you don't know what that is, pretend its a division symbol. Calculate the result as a whole number and take note of the remainder. The remainder is the result of a modulus calculation.
2 % 2 = 0
5 % 3 = 2
When alternating column colors, what we do is set up an incremental counter and use the modulus function to determine whether the counter is an odd or even number. Here is an example of a loop and the modulus calculation:
Code: Select all
for($x = 1; $x<10; $x++){
switch($x % 2){
case 0: echo $x . ' is even. ';
break;
case 1: echo $x . ' is odd. ';
break;
}
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I kind of like using a simple switching mechanism depending on value...
This involves no math. In fact, it is a simple value check that sets a new value if needed.
Code: Select all
<?php
$color = '#ffffff';
for ($i = 0; $i < $array_count; $i)
{
echo '<tr><td bgcolor="' . $color . '">Some content</td></tr>';
if ('#ffffff' == $color)
{
$color = '#cccccc';
}
else
{
$color = '#ffffff';
}
}
?>Or like this:
Edit: I believe that bokehman's way is the best. In the normal way ($i++%2==0) the counter grows and grows... and therefore takes more space in the memory. With bokehman's way, the counter will never be larger than 1:
Code: Select all
echo ($i++ & 1) ? 'color_one' : 'color_two';Code: Select all
echo ($i ^= 1)? 'class_one' : 'class_two';You only need one class. The non-inverted row will then default to the style of a <tr> with no class. Demo... except thats a <ul> styled like that.Oren wrote:Or like this:Code: Select all
echo ($i++ & 1) ? 'color_one' : 'color_two';
Also I don't like the idea of the style being embeded in the tag; I want to be able to control everything to do with style from the stylesheet, not by editing a php file.
If you want to be able to control everything from the CSS file, wouldn't it be better to assign two classes (dark and light) rather than using one class and an unstyled table row?bokehman wrote:You only need one class. The non-inverted row will then default to the style of a <tr> with no class. Demo... except thats a <ul> styled like that.Oren wrote:Or like this:Code: Select all
echo ($i++ & 1) ? 'color_one' : 'color_two';
Also I don't like the idea of the style being embeded in the tag; I want to be able to control everything to do with style from the stylesheet, not by editing a php file.
here's your prize: 