Alternating table styles?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Alternating table styles?

Post by Citizen »

Whats the easiest way to achieve an alternating table style when using a loop that creates new cells?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$on = TRUE;
while(...)
{
  echo "<tr style=\"".($on ? "style1" : "style2")."\">";
   //...
  $on = !$on;
}
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

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

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

}
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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...
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post by neogeek »

Here is a really neat JavaScript solution: http://bitesizestandards.com/bites/auto ... oured-rows
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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 )
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Here is a ready to use PHP solution: http://aidan.dotgeek.org/repos/v/Cycle.php
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post 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>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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. :)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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
Post Reply