Page 1 of 1

Alternating table styles?

Posted: Tue May 23, 2006 12:27 pm
by Citizen
Whats the easiest way to achieve an alternating table style when using a loop that creates new cells?

Posted: Tue May 23, 2006 12:29 pm
by Burrito

Code: Select all

$on = TRUE;
while(...)
{
  echo "<tr style=\"".($on ? "style1" : "style2")."\">";
   //...
  $on = !$on;
}

Posted: Tue May 23, 2006 12:29 pm
by hawleyjr

Code: Select all

for($x=0;$x<30;$x++){
$color = $x % 2?'#FFFFFF':'#000000';

echo '<tr color="' . $color . '"><td>Blah</td></tr>';

}

Posted: Wed May 24, 2006 6:46 pm
by Citizen
hawleyjr wrote:

Code: Select all

for($x=0;$x<30;$x++){
$color = $x % 2?'#FFFFFF':'#000000';

echo '<tr color="' . $color . '"><td>Blah</td></tr>';

}
How does that work? I've never seen a command like that...

Posted: Wed May 24, 2006 7:08 pm
by neogeek
Here is a really neat JavaScript solution: http://bitesizestandards.com/bites/auto ... oured-rows

Posted: Wed May 24, 2006 7:21 pm
by Burrito
Citizen wrote:How does that work? I've never seen a command like that...
have a read on php operators. More specifically look at the ternary and modulus operators.

In all honesty, the modulus is a bit of overkill unless you're planning to alternate between more than just two colors. If you just want to alternate between two colors, look at my example. It's boolean and a little easier to follow (plus it will run faster :P )

Posted: Thu May 25, 2006 4:25 am
by Oren
Here is a ready to use PHP solution: http://aidan.dotgeek.org/repos/v/Cycle.php

Posted: Thu May 25, 2006 5:24 am
by jayshields
Burrito wrote:

Code: Select all

$on = TRUE;
while(...)
{
  echo "<tr style="".($on ? "style1" : "style2")."">";
   //...
  $on = !$on;
}
Very nice. Never seen it done like that before. I always used mod like hawle's example.

Posted: Thu May 25, 2006 9:28 am
by xpgeek
html+php it's bad style use smarty templates and "cycle" function or other template engine.
Example:

Code: Select all

{section name=rows loop=$data}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}">
   <td>{$data[rows]}</td>
</tr>
{/section}

OUTPUT:

<tr bgcolor="#eeeeee">
   <td>1</td>
</tr>
<tr bgcolor="#d0d0d0">
   <td>2</td>
</tr>
<tr bgcolor="#eeeeee">
   <td>3</td>
</tr>

Posted: Thu May 25, 2006 9:38 am
by pickle
1 line can do it:

Code: Select all

while(...)
{
  $class = ($class == 'light') ? 'dark' : 'light';
  echo "<tr class = '$class'><td>blah</td></tr>";
}
Try doing a search for alternating row colours - this has been discussed at length before.

Posted: Thu May 25, 2006 1:10 pm
by Burrito
xpgeek wrote:html+php it's bad style use smarty templates and "cycle" function or other template engine.
HUH??? Templating engines have their place and time, but they certainly should not be used exclusively when combining html and php. 8O

Posted: Thu May 25, 2006 1:25 pm
by Roja
Burrito wrote:
xpgeek wrote:html+php it's bad style use smarty templates and "cycle" function or other template engine.
HUH??? Templating engines have their place and time, but they certainly should not be used exclusively when combining html and php. 8O
Not sure why you are confused ("Huh??"), I'm in agreement with xpgeek.

To me, its the type of problem that yells out for the order and seperation provided by templating engines. Of course, I rarely code without one, so my bias is obviously for templating.

But from the solutions given, his seems to be the most clear, maintainable, and extensible.

Perhaps you could make the argument that the overhead isn't worth it, but is that alternating table the ONLY thing that could use templating? I suspect not.

Good call xpgeek.

Posted: Thu May 25, 2006 1:35 pm
by Burrito
having been exposed to a few templating engines (smarty in particular...thoroughly), and written plenty of code without them, using a templating engine for something so basic as alternating table row colors (the topic of this thread) is absurd. If indeed the OP planned to use multiple interfaces on the front end of a complex application, then yes, a templating engine makes sense. But that is not at all in the scope of this thread.

Posted: Thu May 25, 2006 1:40 pm
by Roja
Burrito wrote:having been exposed to a few templating engines (smarty in particular...thoroughly), and written plenty of code without them, using a templating engine for something so basic as alternating table row colors (the topic of this thread) is absurd.
I have the same opinion about using a ternary and a while loop to accomplish the same task. To me its absurdly ugly and non-extensible. Some people like Chocolate Mint, some people don't. :)

Posted: Thu May 25, 2006 1:40 pm
by MrPotatoes
Citizen wrote:
hawleyjr wrote:

Code: Select all

for($x=0;$x<30;$x++){
$color = $x % 2?'#FFFFFF':'#000000';

echo '<tr color="' . $color . '"><td>Blah</td></tr>';

}
How does that work? I've never seen a command like that...
that's the way that i'm doing mine. but my check is outside the loop