Jump to content

Alternate Row colors from database query


Zergman

Recommended Posts

So I have it working using this

 

<?php
$count++;
if(($count % 2) == 1){
$background = "#CCCCCC";
} else {
$background = "#FFFFFF";
}
?>

 

Then in my tr tag

<tr bgcolor="<?php echo $background; ?>">

 

Works great, except I want the colors to be defined in my stylesheet, not on the page itself so my users can change themes and have the row colors change too.

 

I found a tutorial here http://lorenzod8n.wordpress.com/2007/06/02/alternating-row-color-in-htmlcss-with-php/ but I can't seem to get it to work with a database query.

 

Suggestions?

So I have it working using this

 

<?php
$count++;
if(($count % 2) == 1){
$background = "#CCCCCC";
} else {
$background = "#FFFFFF";
}
?>

 

Then in my tr tag

<tr bgcolor="<?php echo $background; ?>">

 

Works great, except I want the colors to be defined in my stylesheet, not on the page itself so my users can change themes and have the row colors change too.

 

I found a tutorial here http://lorenzod8n.wordpress.com/2007/06/02/alternating-row-color-in-htmlcss-with-php/ but I can't seem to get it to work with a database query.

 

Suggestions?

 

use classes of css

 

eg ..

 

<?php
$count++;
if(($count % 2) == 1){
$class = "style1";
} else {
$class = "style2";
}
?>
<tr class="<?=$class ?>">

Don't use short tags. =P

 

What do you mean?

 

Umm, teng84 used short tags in his code.  Instead of typing <?php echo $variable; ?>, short tags allow you to do <?=$variable?>.  These are not guaranteed to work on all servers, and therefore are considered poor programming practice.  (Wow, that was some pretty good alliteration.  *pats self on back*)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.