Hi, I'm trying to create something like a forum, I want to make it easier for the users to read the post by changing the color of the posts. So the first post will have a background color of #333 and the second one will be #444 and the third one will be again #333. how can I do that? I'm using mysql to display the post Thanks in advance.
Just a quick tutorial I googled for you http://www.codewalkers.com/c/a/Database-Articles/Alternating-row-colors-with-PHP-and-mySQL/1/
During output set your logic as if post number is odd set bgcolor #333 else set bgcolor=#444 to find odd or even you can get the remainder of post number by 2. That will be 0 for even and 1 for odd rows.
class alternator { private $state; public $a; public $b; public function __construct($a = '', $b = '') { $this->a = $a; $this->b = $b; $this->state = false; } public function __toString() { $this->state = !$this->state; return $this->state ? $this->a : $this->b; } } Code (markup): $row_color = new alternator('#900', '#009'); echo "The first color is $row_color<br/>"; echo "The second color is $row_color<br/>"; // Note there would never be an odd number in this situation for($i = 0; $i < 10; $i += 2) { printf('<li style="background-color:%s;">%s</li>', $row_color, $i); } while($row = get_row()) { printf('<li style="background-color:%s;">%s</li>', $row_color, $row); } Code (markup): http://develobert.blogspot.com/2008/01/alternating-php-object.html