Page 1 of 1

Incorporating Alternate bg color rows

Posted: Thu Mar 25, 2004 5:53 pm
by warriors2003
Looking to do an alternating row background color like i have here

http://www.becauseitscool.com/htables22.php

I've looked around, none seem to work with what I already have. I already did it once, but can;t get it to work with a specific query and table that I am using below. It is a plain table with no color in the output echo variables.

What is an easy code to use with the code I have below? And where would it need to go?

<?

while ( $row = mysql_fetch_array($result))

{
echo "<tr><td>$row[against]</td><td>$row[ab]</td><td>$row[h]</td><td>$row[hr]</td><td>$row[rbi]</td><td>$row[doubles]</td><td>$row[triples]</td><td>$row[r]</td><td>$row[sb]</td></tr>";
}
?>

---------------------------------------
Thanks in advance you guys rock!!!

Posted: Thu Mar 25, 2004 5:58 pm
by tim
<td bgcolor=#000000>

=]

Posted: Thu Mar 25, 2004 6:14 pm
by warriors2003
Thats it?? isn;t that just a background color? Do I need to echo it or anything? Where would i put that? Thanks in advance

Posted: Thu Mar 25, 2004 6:23 pm
by phice
You actually alternate a variable for every row...

Code: Select all

<? 

$color = "#F0F0F0";
while ($row = mysql_fetch_array($result)) 
{ 

echo "<tr bgcolor="$color"><td>$row[against]</td><td>$row[ab]</td><td>$row[h]</td><td>$row[hr]</td><td>$row[rbi]</td><td>$row[doubles]</td><td>$row[triples]</td><td>$row[r]</td><td>$row[sb]</td></tr>"; 

 if($color == "#F0F0F0") {
    $color = "#FFFFFF";
 } else {
    $color = "#F0F0F0";
 }

} 
?>
First you set up the variable "color" before the while() loop. Then, in your <tr> tag, put in 'bgcolor=\"$color\"', so it'll set the whole row that specified color. Then, switch the colors around using the if() statement above.

Did this help any?

Posted: Thu Mar 25, 2004 6:30 pm
by Goowe
Phice! Very cool way of doing it! I always thought it had to be done in a much more difficult way :roll: Thanks for posting that code :wink:

Posted: Thu Mar 25, 2004 6:31 pm
by tim
oh I am sorry, if I looked at the link I would understand what you wanted.

Well, this is confusing to me, perhaps this (untested)

Code: Select all

<?php
for($i=1; $i<10; $i++)
{ 
// thats your loop, heres your if statement
if($i%2 == 0){
$bg = "#0000FF";
}else{
$bg = "#FFFFFF";
} 

// notice the %, this tells the server to divide the results by two, if I am correct, which creates two loops in effect

use the llop like this:

echo "<tr bgcolor="$bg"><td >$i</td></tr>";

?>
I think that'll work fo rya

edit - well I think phice method is more clear. lol

Posted: Thu Mar 25, 2004 6:48 pm
by warriors2003
PHICE and TIM MUCH THANKS!!! You guys rock!! This site ROCKS!!! You guys are great

Posted: Thu Mar 25, 2004 7:07 pm
by phice
Goowe wrote:Phice! Very cool way of doing it! I always thought it had to be done in a much more difficult way :roll:
There's millions of different ways of doing the same thing. ;)