PHP Zebra Striping a Table

Table row in a loop:

<!-- Before loop -->
<?php $c = 0; ?>

<!-- Start loop -->
<tr class="<?=($c++%2==1) ? 'odd' : 'even' ?>">
<!-- End loop -->

CSS:

.even { background-color:#FFF; }
.odd { background-color:#666; }

Comments

  1. User Avatar
    Andrew Champ
    Permalink to comment#

    You can even replace “tr” with “div” and do it w/ classes!

  2. User Avatar
    David
    Permalink to comment#

    The only problem in this example is that it will give a notice for the undefined variable $c.

    What I usually do is first initialise the variable to 0 before the start of the loop. Then you won’t get the notice.

  3. User Avatar
    Andrew Champ
    Permalink to comment#

    When I insert this code, it renders it unuseable. Why would it work w/o but not with?

  4. User Avatar
    bucabay
    Permalink to comment#

    The keyword “var” can only be used inside a class declaration.

    $c = 0;

    I think you confused it with the “var” keyword in JavaScript which initializes a variable.
    You can also use the bitwise & to get odd and even.

    eg:


    <?php echo $c++&1 ? 'odd' : 'even'; ?>

    Your example also has the odd and even switched around. Odd numbers will have $c++%2==1, not even.

  5. User Avatar
    Jack

    Please can someone help?

    I put the code into the tr tag on this line and the whole page stops giving an error message with this line number. Also when I put the code into the line the remaining tags don’t work

    echo " \n£ $investment\n";

    Thanks in anticipation

  6. User Avatar
    Artem
    Permalink to comment#

    Cant understand, how it works…

  7. User Avatar
    iTechRoom
    Permalink to comment#

    Very handy and useful trick.

  8. User Avatar
    Erwin
    Permalink to comment#

    what about using array?

    $class = array('even' => 'odd', 'odd' => 'even');
    $curr = 'even';


    <tr class="">

  9. User Avatar
    Erwin
    Permalink to comment#

    sorry, forgot the code wrapper


    <tr class="”>

  10. User Avatar
    Erwin
    Permalink to comment#

    hmm still not working using pre or even code wrapper

    anyway what i do is using $class[$curr] inside my loop

  11. User Avatar
    jennifer
    Permalink to comment#

    Can use this after the “While function”…
    $bgcolor = ($bgcolor == “#ececec”) ? “#ffffff” : “#ececec”;
    then…
    print ”

  12. User Avatar
    apad
    Permalink to comment#

    bucabay, your code works!

  13. User Avatar
    Officer Rex N.Y.P.D
    Permalink to comment#

    zebra table

    .even { background-color:#FFF; }
    .odd { background-color:#666; }

    <?php
    for($a = 1;$a<=10;$a++)
    {
    echo "”;
    echo “”;
    echo “The no is $a”;
    }
    ?>

  14. User Avatar
    newjumanji
    Permalink to comment#

    genius script!!! thank you!

  15. User Avatar
    Tim Smith
    Permalink to comment#

    How do you maintain a consistent zebra pattern in a sortable PHP table?

  16. User Avatar
    Dominor Novus
    Permalink to comment#

    Surprisingly simple. This works great. Thanks.

  17. User Avatar
    Trambulhao
    Permalink to comment#

    $bool = true;

    for ($i = 1; $i <= 10; $i++) :
    echo ‘<span class=”‘ . (($bool) ? ‘odd’ : ‘even’) . ‘”>’ . $i. ‘</span>’;
    $bool = !$bool;
    endfor;

  18. User Avatar
    Kowsick
    Permalink to comment#

    This code is suitable for me. but i don’t know how to insert the css & where i can insert

  19. User Avatar
    DrewChambersDC
    Permalink to comment#

    Easy way to do it with JQuery

    $(document).ready(function() {
    $(“#tableid tr:even”).addClass(“stripe1”);
    $(“#tableid tr:odd”).addClass(“stripe2”);
    });

    /* CSS
    ————- */
    .stripe1 { background-color: # }
    .stripe2 { background-color: # }

  20. User Avatar
    Paul
    Permalink to comment#

    Thank you thank you thank you!

  21. User Avatar
    Justin
    Permalink to comment#

    <?php
    $class = "grey";
    $rowCount = count($rows);
    for ($i = 0; $i
    <tr class="” >Data; ?>

    I feel all code needs to be as readable as possible to eliminate as much need of inline comments as possible, even if the code may be slightly longer.

  22. User Avatar
    Justin
    Permalink to comment#

    <?php
    $class = "grey";
    $rowCount = count($rows);
    for ($i = 0; $i
    <tr class="” >

    For some reason my code was cut by the text parser. If this post doesn’t work, then I am sorry

  23. User Avatar
    Dominic
    Permalink to comment#

    I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get bought an impatience over that you wish be delivering the following.
    unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case
    you shield this increase.

  24. User Avatar
    falcon
    Permalink to comment#

    $a=0;
    while($row = mysql_fetch_assoc($udet))
    {
    if($a++%2==1)
    {
    echo “”.
    “{$row[‘name’]} “.
    “{$row[‘age’]} “.
    “{$row[‘mobile’]} “.
    “{$row[‘date’]} “.
    “{$row[‘address’]} “;

    }else{
    echo “”.
    “{$row[‘name’]} “.
    “{$row[‘age’]} “.
    “{$row[‘mobile’]} “.
    “{$row[‘date’]} “.
    “{$row[‘address’]} “;
    }
    }

  25. User Avatar
    falcon
    Permalink to comment#
    $a=0;
    while($row = mysql_fetch_assoc($udet))
    {
        if($a++%2==1)
        {
        echo "".
            "{$row['name']}  ".
             "{$row['age']} ".
             "{$row['mobile']}  ".
             "{$row['date']} ".
             "{$row['address']}  ";
    
    }else{
         echo "".
            "{$row['name']}  ".
             "{$row['age']} ".
             "{$row['mobile']}  ".
             "{$row['date']} ".
             "{$row['address']}  ";
        }
        }
    
    
    
    
    
  26. User Avatar
    me
    Permalink to comment#

    perfect!

  27. User Avatar
    Anissa

    This code is very simple and yet works well. You are great, very good job! Thanks a lot!

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag