From: Richard Quadling on
On 30 April 2010 08:40, Daevid Vincent <daevid(a)daevid.com> wrote:
>> -----Original Message-----
>> while(foo){
>> $tr = (0 == $trColor % 2)? "#E8E8E8" : "#FFFFFF";
>> echo "<tr style=\"background-color:".$tr."\">";
>> ...
>
> Don't do this modulus (%) math!!!! Just toggle a boolean!!
>
> <tr class="<?= ($r = !$r) ? "dataRow1" : "dataRow2"; ?>">
>

The modulus is a good option when there are more than 2 states.

Say a 5 row fade ...

$a=0;
while ($row=mysql_fetch_...){
echo "<tr class=‌‌‌‌‌‌\"alternate-row-".(1 + (++$a%5))."\"><..."
}

giving alternate-row-1, alternate-row-2, alternate-row-3,
alternate-row-4 and alternate-row-5


--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
From: "Jay Blanchard" on
[snip]
Don't do this modulus (%) math!!!! Just toggle a boolean!!

<tr class="<?= ($r = !$r) ? "dataRow1" : "dataRow2"; ?>">
[/snip]

What is wrong with modulus math????
From: Paul M Foster on
On Thu, Apr 29, 2010 at 05:34:38PM -0400, tedd wrote:

> At 11:01 AM -0300 4/29/10, Juan Rodriguez Monti wrote:

<snip>

>>
>> Tedd,
>> Thanks. I'm gonna check it. I finally solved it using:
>>
>> if ( CONDITIONS )) {
>>
>> $results = query( QUERY );
>> $colors = array('#97b7cd','#96bf8c');
>> $index= 0;
>> echo "<html>";
>> echo "<head>";
>> echo '<link rel="stylesheet" type="text/css" href="is.css" />';
>> echo "</head>";
>> echo "<body>";
>> echo '<div id="container">';
>>
>> echo "<table border='1'>";
>> echo "<td><strong>a1</strong></td> <td><strong>a2</strong></td>
>> <td><strong>a3</strong></td> <td><strong>a4</strong></td>
>> <td><strong>a5</strong></td> ";
>>
>> while ($row = CONDITIONS )) {
>> echo '<tr style="background-color: ' . $colors[$index ++ %
>> 2] . ';">';
>> echo "<td>$row[0]</td><td>$row[1]</td> <td>$row[2]</td>
>> <td>$row[3]</td><td>$row[4]</td> ";
>> }
>> echo "</tr>";
>> echo "</table>";
>> echo "</div>";
>> echo '<p><a href="back.html">back</a></p>';
>> echo "</body>";
>> echo "</html>";
>>
>> This solution was the best for me. It's solved with your help.
>>
>> Thanks a lot!.
>>
>> Juan
>
> The above is far more complicated than it has to be.
>
> Please critically review my example.

+1

This thread came up before, and tedd's solution was the least complex,
as far as I could tell. I shamelessly stole his code and regularly use
it in my own projects. ;-}

Paul

--
Paul M. Foster
From: Ashley Sheridan on
On Fri, 2010-04-30 at 06:14 -0500, Jay Blanchard wrote:

> [snip]
> Don't do this modulus (%) math!!!! Just toggle a boolean!!
>
> <tr class="<?= ($r = !$r) ? "dataRow1" : "dataRow2"; ?>">
> [/snip]
>
> What is wrong with modulus math????
>


Modulus is the most elegant solution I reckon. Imagine if you only
needed to highlight every 3rd row, or 4th? Easy to change the modulus
for it, not so easy to re-work a binary switch.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: Jay Ess on
Paul M Foster wrote:
> +1
>
> This thread came up before, and tedd's solution was the least complex,
> as far as I could tell. I shamelessly stole his code and regularly use
> it in my own projects. ;-}
>
Or if one choose to use Smarty template.
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}">

http://www.smarty.net/manual/en/language.function.cycle.php