scriptygoddess

07 Feb, 2003

Alternate color rows when displaying data

Posted by: Jennifer In: Lessons learned

Again, another "not so interesting for blog stuff" type post. If you're displaying several rows of data and would like to alternate the background colors of the rows so they're easier to read:

<? for ($i=0; $i<$someNumber; $i++) { ?>
<tr <? if ($i%2) { ?> bgcolor="#CCCCCC" <? } ?>>
<td>data</td>
<td>more data</td>
</tr>
<? } ?>

that "$i%2" basically checks to see if $i is even or not, and if it is, print that "bgcolor" thingy so that it will color the background of that row.

2 Responses to "Alternate color rows when displaying data"

1 | Andy

February 8th, 2003 at 6:22 am

Avatar

it took me ages to code something like that in asp – hadn't thought of an if statement like that – doh!

2 | Nathaniel Riethmann

February 12th, 2003 at 7:16 pm

Avatar

A better use for this script would be to assign classes to individual table cells vice hard-coding a background color for them. This allows you to change their background color via CSS and helps separate content from presentation.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements