Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Alternating Row Colors in a Table

    So, I'm having the information fetched from the server and displayed in a table. I'm wanting to know how I could alternate colors in the table. The colors that I want to use are $color1 and $color2. All help would be appreciated! Thanks! :)
    PHP Code:
    <?php

    $color1 
    "#FFFFFFF";
    $color2 "#C8C8C8";

    $test=$_GET['test'];
    $section=$_GET['section'];
    $sql="SELECT * FROM `tests` WHERE `id`='".$test."' AND `section`='".$section."' ORDER BY year DESC";
    $res=mysqli_query($con,$sql)or die(mysql_error());
    if(
    mysqli_num_rows($res) == 0){
        echo 
    "No tests associated with this url.\n";
    }else {
        echo 
    "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"100%\">\n";
        echo 
    "<tr><td>Year</td><td>Test</td><td>Key</td><td>Solution set</td><td>Take Online</td><td>First</td><td>Second</td><td>Third</td></tr>\n";
        while(
    $row mysqli_fetch_assoc($res)){
            echo 
    "<tr><td>".$row['year']."</td><td>";
    if(
    $row['link_t'] == 'Link' or $row['link_t'] == 'UNAVAILABLE'){
    echo 
    "UNAVAILABLE";
    }else{
    echo 
    "<a href=\"".$row['link_t']."\">Test</a></td>";
    }
    if(
    $row['link_k'] == 'Link' or $row['link_k'] == 'UNAVAILABLE'){
    echo 
    "UNAVAILABLE";
    }else{
    echo 
    "<td><a href=\"".$row['link_k']."\">Key</a></td>";
    }
    if(
    $row['link_s'] == 'UNAVAILABLE' or $row['link_s'] == 'UNAVAILABLE'){
    echo 
    "<td>UNAVAILABLE</td>";
    }else {
    echo 
    "<td><a href=\"".$row['link_s']."\">Solution Set</a></td>";
    }
    if(
    $row['link_o'] == 'UNAVAILABLE' or $row['link_o'] == 'UNAVAILABLE'){
    echo 
    "<td>UNAVAILABLE</td>";
    }else {
    echo 
    "<td><a href=\"".$row['link_o']."\">Solution Set</a></td>";
    }
    echo 
    "<td>".$row['score_f']."</td><td>".$row['score_s']."</td><td>".$row['score_t']."</td></tr>\n";
        }
        echo 
    "</table>\n";
    }

    ?>

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    This would be better to be done with CSS rather than PHP, it saves you headaches and makes life a lot easier because all you have to do is give your table a class rather than doing this method which takes a (very small) amount of processing, depending on how much there is. CSS would just be easier for you in my opinion. Even if you don't know how to create CSS, doing this shouldn't be too hard to do, I'm sure there's many tutorials on doing this with CSS.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    CSS

    Code:
    tr:nth-child(even) {background: #C8C8C8}
    tr:nth-child(odd) {background: #FFF}
    and no change to the table in your PHP at all.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Posts
    462
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Code:
    ...
    $i=0;
    while($row = mysqli_fetch_assoc($res)){
    $alternate = ($i % 2) ?   "#FFFFFFF": "#C8C8C8";
    echo '<tr ' .$alternate. '>';
    
    ...
    
    $i++;
        }//[end whileLoop]
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    For projects using MediaTypes (MIMETypes) visit E-BAM.net -(updated weekly)

    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #5
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    CSS

    Code:
    tr:nth-child(even) {background: #C8C8C8}
    tr:nth-child(odd) {background: #FFF}
    and no change to the table in your PHP at all.
    I was hoping to avoid doing that, since it changes all tables to that method.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Burklow View Post
    I was hoping to avoid doing that, since it changes all tables to that method.
    So give the table you want to apply it to an id and add the id to the front of each of the two lines.

    <table id="stripe">

    and

    Code:
    #stripe tr:nth-child(even) {background: #C8C8C8}
    #stripe tr:nth-child(odd) {background: #FFF}
    or if you want it to apply to more than one table but not all then make it a class and replace the # with a .
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    So give the table you want to apply it to an id and add the id to the front of each of the two lines.

    <table id="stripe">

    and

    Code:
    #stripe tr:nth-child(even) {background: #C8C8C8}
    #stripe tr:nth-child(odd) {background: #FFF}
    or if you want it to apply to more than one table but not all then make it a class and replace the # with a .
    I didn't realize that you could have an id defined twice. Thanks for the help!

  • #8
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Burklow View Post
    I didn't realize that you could have an id defined twice. Thanks for the help!
    You shouldn't. You're technically able to but doing so is a big nono. Use the class as mentioned before instead.

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by Burklow View Post
    I didn't realize that you could have an id defined twice.
    that’s not an ID definition, that’s CSS selectors using an ID.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

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