SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how do i make each <tr> a different bgcolor when querying mysql

    I am querying data from a database and displaying it in a table like this:

    PHP Code:
    <table> 
     
    <?php
    $sql 
    mysql_query("SELECT * FROM db1"); 

    while (
    $row mysql_fetch_array($sql)) {
     
    $other $row['other'];
     
    $number $row['number'];
      echo 
    "<tr valign=top>\n";
      echo 
    "<td class=text>$other</td>\n";
      echo 
    "<td class=text>$number</td>\n";   
      echo 
    "</tr>\n";
    }
    ?>
     
    </table>
    What I would like to do is have every other <tr> have a light gray bgcolor... for easy reading. Can someone help me? I'm not sure how to do this.

    Thanks!
    JP

  2. #2
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    <table cellspacing="0" cellpadding="0">
    <?php
    $sql 
    mysql_query("SELECT * FROM db1");

    $color_switch 1;
    while (
    $row mysql_fetch_array($sql)) {
       
    $color = ($color_switch==1) ? "#aabbcc" "#ffaaff"
       
    $color_switch = -$color_switch;

       echo 
    "<tr valign=top>\n";
          echo 
    "<td class=text style='background-color:$color'>"$row['other'], "</td>\n";
          echo 
    "<td class=text style='background-color:$color'>"$row['number'], "</td>\n";   
       echo 
    "</tr>\n";
    }
    ?>
    </table>
    Edit:


    Code adjusted for brevity.
    Last edited by lieut_data; Aug 7, 2003 at 14:43.
    My name is Steve, and I'm a super-villian.

  3. #3
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <table> 

    <?php
    $sql 
    mysql_query("SELECT * FROM db1" ); 

    $color '#ffffff';
    while (
    $row mysql_fetch_array($sql)) {
    $other $row['other'];
    $number $row['number'];
    $color = ($color == '#f0f0f0') ? '#ffffff' '#f0f0f0';
    echo 
    "<tr valign=top>\n";
    echo 
    "<td class=text bgcolor=\"$color\">$other</td>\n";
    echo 
    "<td class=text bgcolor=\"$color\">$number</td>\n"
    echo 
    "</tr>\n";
    }
    ?>

    </table>
    That's the way I always do it, except generally I use CSS classes:
    PHP Code:
    $color = ($color == 'cellmain') ? 'cellalt' 'cellmain'
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $count = 0;
    while($row = $iterate -> getCurrent()) {
    $iterate -> getNext();
    .
    .
    $bgcolor = ($count % 2)? '#ff0000':'#00ff00';
    ?>
    <tr>
    <td bgcolor="<?php echo($bgcolor); ?>"> ... </td></tr>
    .
    .
    .
    <?php
    $count
    ++;
    }

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see the need for PHP to perform calculations on that, when all it needs to do is an if/else check, but oh well
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome! That's great... I'm already using it.

    one problem though... i'm going to be having several people print this page-- when i print it like above the highlighted rows do not print. is there any easy fix? i know there are settings in IE i assume i could turn on... but i'd rather it print no matter what.

  7. #7
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How the page prints is entirely up to the user -- Only they can enable color / background-color printing on their system.
    My name is Steve, and I'm a super-villian.

  8. #8
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Lange
    PHP Code:
    $color '#ffffff';
    $color = ($color == '#f0f0f0') ? '#ffffff' '#f0f0f0'
    Why repeat colors twice?
    My name is Steve, and I'm a super-villian.

  9. #9
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't really need the
    PHP Code:
    $color '#ffffff'
    But I add it, because it is necessary when using E_ALL error_reporting.

    If you remove that, all you have is

    PHP Code:
    $color = ($color == '#f0f0f0') ? '#ffffff' '#f0f0f0'
    Which is just an if/else statement, and doesn't require PHP to do any calculations like this does:

    PHP Code:
    $color_switch 1;
    if (
    $color_switch == 1) {
        
    $color '#ffffff';
    } else {
        
    $color '#f0f0f0';
    }
    $color_switch = -$color_switch
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  10. #10
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, je comprend!

    Thanks!
    My name is Steve, and I'm a super-villian.


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
  •