Yet even more ways to do it:

 $i = 0;
 while ($row = mysql_fetch_assoc($result)) {
     $bgcolor = ($i++ & 1) ? '#ffffff' : '#000000';
 }

Or more then two:

 $colors = array('black','green','blue','yellow');

 $i = 0;
 while ($row = mysql_fetch_assoc($result)) {
    $bgcolor = $colors[$i++ % 4];
 }

The first used &, which is a bitwise operator. See 
also the following faqt:

 How can I show rows from a database in a table with 
 alternating colors?

 http://www.faqts.com/knowledge_base/view.phtml/aid/783

Regards,
Philip Olson


On Fri, 10 May 2002, Miguel Cruz wrote:

> While everyone's kicking in ideas...
> 
> I don't think modulus (as someone else suggested) is that expensive, but I 
> think this is the cheapest:
> 
>   $evenodd = 0;
>   $color[0] = '#cccccc'; 
>   $color[1] = '#999999';
>   while ($row = mysql_fetch_row($st))
>   {
>     print "<tr><td bgcolor={$color[$evenodd]}>{$row['whatever']}</td></tr>";
>     $evenodd = 1 - $evenodd;
>   }
> 
> miguel
> 
> On Fri, 10 May 2002, Jay Blanchard wrote:
> 
> > [snip]
> > Is there a way for me to do one of the following:
> > 
> > 1) Test to see if $i is an even or odd number?
> > 2) Grab more than one line from the database at a time, and just put in two
> > table rows at once?
> > [/snip]
> > 
> > <?
> > $rowcount = 0;
> > while($dbrowa = mysql_fetch_object($dbseta)){
> >         $dbrowb = mysql_fetch_object($dbsetb);
> > //change the color of alternating rows
> > $rowcount ++;
> > if ($rowcount == 1){
> >         print("<tr bgcolor=\"#FFFF99\">\n");
> >         }
> >         elseif ($rowcount <> 1) {
> >                 print("<tr>\n");
> >                 $rowcount = 0;
> >         }
> > 
> > 
> > This way you don't have to know even or odd.
> > $rowcount is 0
> > $rowcount is set to one and printed in a different color
> > $rowcount is set to two, which is not equal to one, so does elseif and is
> > set to zero
> > 
> > HTH!
> > 
> > Jay
> > 
> > 
> > 
> > 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to