Alternating color display

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
Reed
Forum Newbie
Posts: 3
Joined: Thu Oct 10, 2002 1:42 pm

Alternating color display

Post by Reed »

Hi,

I came across this script from evilwalrus

Code: Select all

<?
$result = mysql_query (SELECT * FROM table); 

if ($row = mysql_fetch_array($result)) { 

    for ($i = 0; i < count($row); $i++) { 
     
        // Set Table Row Color 
     
        if ($i % 2) { 
         
            echo "<tr bgcolor=B0C4DE>"; 
            } else { 
            echo "<tr bgcolor=FOF8FF>"; 
        } 

    // Now display our table cell info 
?> 

<td><? echo $rowї"whatever"]; ?></td> 
</tr> 

<? 
    } else { 
     
    echo "<tr bgcolor=B0C4DE><td>Sorry, no records were found!</td></tr>"; 
     
    } 
         
?>
I would like my data to be displayed in this format :

Code: Select all

<TABLE><TR><TD align=center>Title</TD></TR></TABLE>
<TABLE><TR><TD>Username: </TD></TR></TABLE>
<TABLE><TR><TD>Date: </TD></TR><TABLE>
<TABLE><TR><TD>Tel: </TD></TR></TABLE>
<TABLE><TR><TD>Website: </TD></TR></TABLE>
<TABLE><TR><TD>Price: </TD></TR></TABLE>
<TABLE<TR><TD>Email: </TD></TR></TABLE>
<TABLE><TR><TD align=center>Ad Description</TD></TR></TABLE>
How do i modify it to fit this portion:

Code: Select all

<td><? echo $rowї"whatever"]; ?></td> 
</tr>
Please advise.

Thanks :D
SuperHuman
Forum Newbie
Posts: 10
Joined: Wed Oct 16, 2002 10:00 pm

Post by SuperHuman »

Well, that script that you are showing is inefficiant as it uses php modulus (%) and I would not reccomend using it for alot of returning rows. Your best bet would be to use the bitwise (&) operator for alternating row colors. Search the forum here, I am sure someone has posted an efficient way to alternate row colors......
Post Reply