SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me with a PHP code that will be changing tables rows colors

    Hello People,
    Help me with a PHP code that will be changing tables rows colors as they being displayed. I have sql that is pulling records from a database and being displayed on a table on web page in rows. Now I want PHP or java scripts that make one row appear as red then next row as blue and next red just like that.

    Thanks in advance.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Create 2 styles for table rows in your CSS, one with background color red, the other blue.
    Then, in the loop that creates the table, use a variable (boolean for example) to decide which style to give to the current row.

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you want advice, how to do it or ready-made code?
    That is very simple though.
    A variable and a condition.
    PHP Code:
    if ($color=="blue"$color="red";
    else 
    $color="blue"
    Hope you have an idea, how PHP works, to paste this code into right place.

  4. #4
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please give an example. I have not manage to figure out the way to do it

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi zamzam,

    $rowColour = 0;

    while($myrow = mysql_fetch_array($find_rows)){

    echo "<div style=\"background:" . ($rowColour&#37;2 ? '#000' : '#fff') . ";\">" . $myrow['data'] . "</div>";

    $rowColour++;

    }

    I'm sure there are more ways to do it but that one should work and I find to be quite compact.

    Regards,


    David
    David McLeary
    Emerging Innovations
    Website Development, Training and Consultancy

  6. #6
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple Example with for loop:
    PHP Code:
    <?php
    $num 
    1;
    echo 
    "<table border='1'>";
    $no 1;
    for(
    $num$num <= 6$num++){
    if(
    $no &#37; 2 == 0){
    $font ="red";
    }
    elseif(
    $no == 1){
    $font ="blue";
    }
    echo 
    "<tr>";
    echo 
    "<td><font color=$font>name</font></td>";
    echo 
    "</tr>";

    $no++;
    }
    echo 
    "</table>";
    ?>
    While loop with query:
    PHP Code:
    <?php
    echo "<table border='1'>";
    $no 1;
    while(
    your query){
    if(
    $no == 0){
    $font ="red";
    }
    elseif(
    $no == 1){
    $font ="blue";
    }
    echo 
    "<tr>";
    echo 
    "<td><font color=$font>name</font></td>";
    echo 
    "</tr>";

    $no++;
    }
    echo 
    "</table>";
    ?>
    You can set it for background of the rows.

  7. #7
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can use js

    Here is how I would do it in jquery:
    Code JavaScript:
    $("tr:even").css("background-color", "red");
    $("tr:odd").css("background-color", "blue");

  8. #8
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way i do is to use the function as follow:

    //usage
    PHP Code:
    $i = 0;
    foreach($array as $key => $val){
    ?>
    <tr<?=altColor($i)?>>
    ....
    </tr>
    <?php
     $i
    ++;
    }
    function:
    PHP Code:
    function altColor($count$row1 "class1"$row2 "class2"){
        
    $row "";
        if(
    $count&#37;2 == 0){
            
    $row " class='".$row1."'";
        } else{
            
    $row " class='".$row2."'";
        }
        return 
    $row;


  9. #9
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    The way i do is to use the function as follow:

    //usage
    PHP Code:
    $i = 0;
    foreach($array as $key => $val){
    ?>
    <tr<?=altColor($i)?>>
    ....
    </tr>
    <?php
     $i
    ++;
    }
    function:
    PHP Code:
    function altColor($count$row1 "class1"$row2 "class2"){
        
    $row "";
        if(
    $count%== 0){
            
    $row " class='".$row1."'";
        } else{
            
    $row " class='".$row2."'";
        }
        return 
    $row;

    Additionally you can do alternation @ load time using javascript(jquery etc.)

  10. #10
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Andrew E. View Post
    Or you can use js

    Here is how I would do it in jquery:
    Code JavaScript:
    $("tr:even").css("background-color", "red");
    $("tr:odd").css("background-color", "blue");
    Use jQuery, and your site will be much simpler.

  11. #11
    SitePoint Zealot kamm's Avatar
    Join Date
    Nov 2003
    Location
    Espaņa
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    This is the one I like - stick this in your loop...

    PHP Code:
    $class = ($class == 'even') ? 'odd' 'even'
    Code HTML4Strict:
    <tr class="$class">

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)

  13. #13
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not much of these numerous advisors ever think of template use

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    PHP Code:
    <?php
    $arrRows 
    = array('one','two','three','four','five','six');
    $cycle false;
    ?>
    <table>
        <thead>
            <tr>
                <th>Num</th>
            </tr>
        </thead>
        <tbody><?php
        
    foreach($arrRows as $arrRow) {
            
    printf('<tr style="background-color:%s;"><td>%s</td></tr>',(($cycle =! $cycle)?'red':'yellow'),$arrRow);
        }
        
    ?></tbody>
    </table>


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
  •