SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot amri's Avatar
    Join Date
    Dec 2002
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Different table cell color for each record

    I'm new to PHP. How to display record from database in a table so the table cell background color for first record will be white and the next record will be gray and the third record will be white again and so on.

    Big Thanks

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    	
    <?
    $i = 0;
    while ($row = mysql_fetch_array($rows))
    {
      $color = ($i % 2 == 0) ? "#ffffff" : "#808080";
      $i++;
      // ...and get values for this row...
    ?>
    <tr bgcolor="<?=$color?>">
      <td>Blah...</td>
      <!-- More columns... -->
    </tr>
    <?
    }
    ?>
    % means "modulo"
    Examples:
    0 mod 2 == 0
    1 mod 2 == 1
    2 mod 2 == 0
    3 mod 2 == 1
    etc

  3. #3
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just for clarification to anyone who may not know, modulus takes the remainder of any two numbers. You may also benefit by looking closer at Jofa's examples. Specifically, note the smaller number divided by the larger number. This returns basically the difference between the two numbers as the remainder (since a larger number cannot go into a smaller number). Just information for your benefit.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  4. #4
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just for clarification to anyone who got confused by Aes' post, modulus takes the remainder after dividing any two numbers

    And, if you in another situation, for some reason, use the modulus operator on negative numbers, be aware that different languages have different rules (!)

  5. #5
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jofa
    Just for clarification to anyone who got confused by Aes' post, modulus takes the remainder after dividing any two numbers ...
    Well duh!! It was implied, you know. Dunno' about you, but when a remainder is taken, that usually implies some dividision has taken place.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  6. #6
    SitePoint Member
    Join Date
    May 2002
    Location
    Ljubljana, Slovenia
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jofa
    Code:
    	 
    <? 
    $i = 0; 
    while ($row = mysql_fetch_array($rows)) 
    { 
    $color = ($i % 2 == 0) ? "#ffffff" : "#808080"; 
    $i++; 
    // ...and get values for this row... 
    ?> 
    <tr bgcolor="<?=$color?>"> 
    <td>Blah...</td> 
    <!-- More columns... --> 
    </tr> 
    <? 
    } 
    ?>
    % means "modulo"
    Examples:
    0 mod 2 == 0
    1 mod 2 == 1
    2 mod 2 == 0
    3 mod 2 == 1
    etc
    Here's even a cleaner solution

    Code:
    	 
    <? 
    $color = array( '#ffffff', '#808080' ); 
    while ($row = mysql_fetch_array($rows)) 
    { 
    ?> 
    <tr bgcolor="<?=$color[0]?>"> 
    <td>Blah...</td> 
    <!-- More columns... --> 
    </tr> 
    <?
    $color = array_reverse( $color );
    } 
    ?>

  7. #7
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •