Next post a different color

Discussion in 'PHP' started by red-x, Oct 4, 2008.

  1. #1
    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.
     
    red-x, Oct 4, 2008 IP
  2. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
  3. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #3
    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.
     
    spc, Oct 4, 2008 IP
  4. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you guys I'll try both. :)
     
    red-x, Oct 5, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    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
     
    joebert, Oct 5, 2008 IP
  6. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for that joebert :)
     
    red-x, Oct 5, 2008 IP