944,198 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2899
  • PHP RSS
Oct 29th, 2007
0

Displaying Mysql rows, alternate colors?

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Oct 29th, 2007
0

Re: Displaying Mysql rows, alternate colors?

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:

PHP Syntax (Toggle Plain Text)
  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 6:19 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rankenian is offline Offline
4 posts
since Oct 2007
Oct 29th, 2007
0

Re: Displaying Mysql rows, alternate colors?

Thanks, that worked great!
Last edited by nathanpacker; Oct 29th, 2007 at 6:44 pm.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem with inserting data into mysql with PHP
Next Thread in PHP Forum Timeline: Newbie Needs Form Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC