Incorporating Alternate bg color rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Incorporating Alternate bg color rows

Post 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!!!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

<td bgcolor=#000000>

=]
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Post 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
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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?
Image Image
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post 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:
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
warriors2003
Forum Commoner
Posts: 38
Joined: Wed Mar 24, 2004 7:24 pm

Post by warriors2003 »

PHICE and TIM MUCH THANKS!!! You guys rock!! This site ROCKS!!! You guys are great
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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. ;)
Image Image
Post Reply