table row alternating colors

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
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

table row alternating colors

Post by meander »

i got this code out of a book to alternate the colors of the table rows

Code: Select all

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
		$bg = ($bg=='dddddd' ? 'ffffff' : 'dddddd');
		
		echo "<tr bgcolor="", $bg, "" ><td align="left">$row[0]</td><td align="left">$row[1]</td><td align="left">$row[2]</td><td align="left">$row[3]</td></tr>\n";
	}
and i was wondering what it actually does? it seems like it does something like if $bg = dddddd then switch it to ffffff and vice versa. am i completely wrong about this?
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post by meander »

i also forgot to ask, can this same method be applied to different things? like anything that you would need to alternate?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes. it flips between 2 colors for the rows output.

This kind of thing works for plenty of other things.
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post by meander »

so then if i wanted to flip back and forth between a variable i could just do

Code: Select all

$var = ($var=='one' ? 'two' : 'one');
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's one way of doing it, yes.
Post Reply