Robert Kern
PHP Web Developer
My name is Robert Kern.
I am a PHP developer from Auckland, New Zealand.
Browse through some of my web development projects, or get in contact with me if you are interested in hiring my php web development services.

Search

  • Topics

    • Honduras (25)
    • Hosting (3)
    • How to (9)
    • iphone (11)
    • Mac (8)
    • Movies (1)
    • Off Topic (10)
    • PHP (8)
    • Recipes App (12)
    • Servers (1)
    • Software (8)
    • Subversion (4)
    • Technology (4)
    • Uncategorized (1)
    • Videos (8)
    • Web Development (19)
      • Security (2)

 

Archives

  • March 2010
  • December 2009
  • November 2009
  • October 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • July 2007
  • Home
  • Web development
  • PHP
  • Projects
  • Contact me
...
  • In Honduras
« Across the universe
Move data from one web server to another using wget »

Use PHP to make alternate coloured rows

Home > PHP > Use PHP to make alternate coloured rows

I came across an article that described how to use PHP to make alternate coloured rows in a table and was surprised at how difficult they made it.  There is a very simple way to display alternate coloured rows in tabular data.  Read on for more info.

Use the following PHP code to display alternate rows in different colours.  What this is actually doing is setting the class for one row to: odd, and the next row to: even.  Then in my css stylesheet I will define .odd and .even to have different background colours.  I see this as a very clean and manageable way to do this common task.

<?php
$i = 0; //Set the index to zero
//Put your code here to loop through the database result or whatever it is you are displaying
//Then display the table row
?>
<tr class="<?php if($i%2){ echo 'odd'; } else { echo 'even'; } ?>">

This same idea can be used in probably all web development coding languages with slightly different syntax.

Tags: CSS, PHP, Tables

This entry was posted on Friday, August 29th, 2008 at 2:56 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a comment

© 2004-2010 Robert Kern, owner of Frondiz, need Hosted Subversion? | Design by Jarred Bishop | WordPress