Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 10th Feb 2011, 11:24 PM   #1
PeaceRoom Member
 
webpro4hire's Avatar
 
Join Date: 2011
Location: Montreal, Canada
Posts: 85
Thanks: 12
Thanked 9 Times in 9 Posts
Default
PHP Quicky - Alternate table row colors
Share on: 
fb share twitter share gplus share more share

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
webpro4hire is offline  
Unread 10th Feb 2011, 11:27 PM   #2
WebNinja
 
jminkler's Avatar
 
Join Date: 2010
Location: Atlanta GA
Posts: 444
Thanks: 19
Thanked 54 Times in 34 Posts
Default
Re: PHP Quicky - Alternate table row colors
Share on: 
fb share twitter share gplus share more share

Should add $clrCounter = 0; first
jminkler is offline  
Closed Thread


Bookmarks

Tags
alternate, colors, php, quicky, row, table


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 10:04 PM.