Alternating table styles?
Posted: Tue May 23, 2006 12:27 pm
Whats the easiest way to achieve an alternating table style when using a loop that creates new cells?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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>'; }
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...
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; }
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>Code: Select all
while(...)
{
$class = ($class == 'light') ? 'dark' : 'light';
echo "<tr class = '$class'><td>blah</td></tr>";
}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.
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.
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.
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>'; }