PHP Quicky - Alternate table row colors

1 replies
Good day fellow warrior programmers,

Time for another simple PHP hack. This time, instead of explaining a function and how to use, I will give you a simple, one liner for alternating colors in table rows.

You see this often in tables with lots of data, rows alternate color (white/grey for example). Designers do this to make it easy for the eye to follow a row. It's a quick visual guide for accuracy.

so without further ado, here's the code:

Code:
<tr bgcolor="<?php echo ($clrCounter++ % 2 == 0 ? 'color1' : 'color2'); ?>">

color 1, color 2 should be HEX codes (ie. #ffffff, #ff0000)
basically, $clrCOunter is incremented after each pass, hence we check the remainder of the counter divided by 2. Every second pass will return a reminder of 0, therefore alternating colors.

Enjoy!
WebPro
#alternate #colors #php #quicky #row #table
  • Profile picture of the author jminkler
    Should add $clrCounter = 0; first
    {{ DiscussionBoard.errors[3345125].message }}

Trending Topics