Forum Moderators: coopster

Message Too Old, No Replies

Shading every other row of repeat region

How to?

         

megajam02

1:00 am on Aug 5, 2004 (gmt 0)

10+ Year Member



How would I go about shading every other row of a repeat region say, light gray?

Birdman

1:50 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



During your PHP loop, you need to check an index(counter) variable to see if it's even or odd. I use the is_float [php.net]() function to check it.

Example in a while() loop:

$counter = 0;

while ($i = mysql_fetch_array($result)){

$counter++; // increases by one on each iteration of loop

if (is_float($counter/2)) $class = "gray";
else $class = "white";

print "<td class=\"".$class."\">stuff</td>"; // either gray or white

}

Welcome to Webmaster World, megajam02!

Birdman

httpwebwitch

5:05 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have another favourite method:

$color1="#FFFFFF";
$color2="#EEEEEE";

echo "<td bgcolor='$bgcolor'>";

$bgcolor=($bgcolor==$color1)?$color2:$color1;
// this "toggles" the color

echo "<td bgcolor='$bgcolor'>";

Robber

8:45 am on Aug 5, 2004 (gmt 0)

10+ Year Member



Or you could do:

$counter = 0;

while ($i = mysql_fetch_array($result)){

$counter++; // increases by one on each iteration of loop

if ($counter%2===0){
$class = "gray";
}else{
$class = "white";
}
print "<td class=\"".$class."\">stuff</td>"; // either gray or white

}

TheBlueEyz

10:42 am on Aug 5, 2004 (gmt 0)

10+ Year Member



Or yet one more version of the testing for even/oddness...

$counter = 0;

while(etc)
{

/*will be true for odd numbers
change to $counter & 0 if you want it to be true
for even numbers */

if($counter & 1)
{
etc...
}
$counter++;

}

Remember these are binary. Numbers will go

0000
0001
0010
0011

etc., and the LSD will alternate 0/1. This is the fastest method of testing for an even or odd number (using a bitwise and). Using MOD is a good way to do it, but it's slow.

megajam02

1:21 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Thanks for the suggestions guys. I tried each of your examples, but none worked. Im sure I put something in the wrong place, or put an extra bracket, quotation mark or something.

Here is my original code:

<?php do {?>
<tr>
<td height="16"><span class="style20"><?php echo $row_Recordset1['username'];?></span></td>
<td><span class="style20"><?php echo $row_Recordset1['user_firstname'];?></span></td>
<td><span class="style20"><?php echo $row_Recordset1['user_lastname'];?></span></td>
<td><span class="style20"><?php echo $row_Recordset1['user_website'];?></span></td>
<td><span class="style20"><?php echo $row_Recordset1['user_email'];?></span></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));?>

Would anyone be willing to edit it for me so I can see exactly how to use your suggestion.

coopster

2:28 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You will (likely) want the class on the table row.

Robber

4:13 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Yeah nice one TheBlueEyz, I remember looking at that a while ago but forgot about it, think I might start using it if its quicker.

Cheers

Birdman

4:27 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, here's an example...using TheBlueEyz' method(nice):

<?php
$counter = 0;
do {
$color = ($counter & 1)? "#fff" : "#666";
$counter++
?>
<tr style="background: <?php print $color;?>;">
...
...
...
...
</tr>
<?php
} while (
$row_Recordset1 = mysql_fetch_assoc($Recordset1)
)
;

Not sure if that last semicolon is needed.