Page 1 of 1

table row alternating colors

Posted: Sun Oct 10, 2004 10:00 pm
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?

Posted: Sun Oct 10, 2004 10:04 pm
by meander
i also forgot to ask, can this same method be applied to different things? like anything that you would need to alternate?

Posted: Sun Oct 10, 2004 11:07 pm
by feyd
yes. it flips between 2 colors for the rows output.

This kind of thing works for plenty of other things.

Posted: Sun Oct 10, 2004 11:18 pm
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');

Posted: Sun Oct 10, 2004 11:19 pm
by feyd
that's one way of doing it, yes.