SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alternating Row Backgrounds

    Hi all,

    If I execute a MySQL query to select and display a number of records in a table, how can I make it so that every other row is grey? Rather like the links to the latest topics on the Sitepoint home page.

    Using a while() loop would be preferred as I am familiar with these, but if not any old way will do. Thanks in advance

    -Sam
    Sam Hastings

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you are looking for?

    PHP Code:
    $dark 1;
    while( 
    condition )
    {
      if( 
    $dark == )
      {
        
    $background '#colorNumbera';
        
    $dark 0;
      }
      else
      {
        
    $background '#colorNumber2';
        
    $dark 1;
      }
      echo 
    '<tr bgcolor=".'$background.'">';
      
      [
    the rest of the cond in the loop]

    John

  3. #3
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Count the number of rows (variable $x) in the while loop, and check if $x % 2 == 0 or not (odd/even), then apply different css class (or bgcolor) for the <tr>

    (% == mod operator, remainder after division)

  4. #4
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check this thread in the tutorials forum:

    http://www.sitepointforums.com/showt...threadid=76466

    Matt.


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
  •