Alternating table styles?
Moderator: General Moderators
Alternating table styles?
Whats the easiest way to achieve an alternating table style when using a loop that creates new cells?
Code: Select all
$on = TRUE;
while(...)
{
echo "<tr style=\"".($on ? "style1" : "style2")."\">";
//...
$on = !$on;
}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...hawleyjr wrote:Code: Select all
for($x=0;$x<30;$x++){ $color = $x % 2?'#FFFFFF':'#000000'; echo '<tr color="' . $color . '"><td>Blah</td></tr>'; }
Here is a really neat JavaScript solution: http://bitesizestandards.com/bites/auto ... oured-rows
have a read on php operators. More specifically look at the ternary and modulus operators.Citizen wrote:How does that work? I've never seen a command like that...
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
Here is a ready to use PHP solution: http://aidan.dotgeek.org/repos/v/Cycle.php
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Very nice. Never seen it done like that before. I always used mod like hawle's example.Burrito wrote:Code: Select all
$on = TRUE; while(...) { echo "<tr style="".($on ? "style1" : "style2")."">"; //... $on = !$on; }
- xpgeek
- Forum Contributor
- Posts: 146
- Joined: Mon May 22, 2006 1:45 am
- Location: Kyiv, Ukraine
- Contact:
html+php it's bad style use smarty templates and "cycle" function or other template engine.
Example:
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>1 line can do it:
Try doing a search for alternating row colours - this has been discussed at length before.
Code: Select all
while(...)
{
$class = ($class == 'light') ? 'dark' : 'light';
echo "<tr class = '$class'><td>blah</td></tr>";
}Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Not sure why you are confused ("Huh??"), I'm in agreement with xpgeek.Burrito wrote:HUH??? Templating engines have their place and time, but they certainly should not be used exclusively when combining html and php.xpgeek wrote:html+php it's bad style use smarty templates and "cycle" function or other template engine.
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.
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.
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.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.
- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
that's the way that i'm doing mine. but my check is outside the loopCitizen wrote:How does that work? I've never seen a command like that...hawleyjr wrote:Code: Select all
for($x=0;$x<30;$x++){ $color = $x % 2?'#FFFFFF':'#000000'; echo '<tr color="' . $color . '"><td>Blah</td></tr>'; }