Displaying Mysql rows, alternate colors?

Please support our PHP advertiser: 50% off 6 Months Dedicated Server Hosting from 1&1!
Reply

Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Displaying Mysql rows, alternate colors?

 
0
  #1
Oct 29th, 2007
Hey, I'm using a while loop to display all the rows of a table in my database, and wondered if anyone knows how I can make it so the background color of each row alternates between two different colors. I'm using php to call the information, so I can stick some HTML in there if needed, but is this possible?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: Rankenian is an unknown quantity at this point 
Solved Threads: 0
Rankenian Rankenian is offline Offline
Newbie Poster

Re: Displaying Mysql rows, alternate colors?

 
0
  #2
Oct 29th, 2007
It can be done, just store the colour you want in a variable and swap it with a 2nd colour every time you go around the loop

Something like this:

  1. $bg = '#FF0000';
  2. while ($row = mysql_fetch_array($result)) {
  3. if ($bg == '#FF0000') $bg = 'FFFF00';
  4. else $bg = '#FF0000';
  5. $output .= "<tr bgcolor=\"#" . $bg . "\">" . "<td>" . $row['data'] . "</td></tr>"
  6. }
  7. echo $output;
Last edited by Rankenian; Oct 29th, 2007 at 5:19 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Displaying Mysql rows, alternate colors?

 
0
  #3
Oct 29th, 2007
Thanks, that worked great!
Last edited by nathanpacker; Oct 29th, 2007 at 5:44 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2373 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC