Viz Viz - 11 months ago 47
PHP Question

PHP, alternate table row colors in while loop

I am trying to alternate row colours in a while loop. I have no idea why this code isn't working. It just shows the first row as black and all others as white. (I know the coding is a bit crappy, trying to learn as I build things) Any explanation on why it doesn't work is greatly appreciated. Thanks.

echo "<table border=1>";


while($rows < 10) {
echo "<tr bgcolor=$bgcolor><td width=100> test </td> </tr>";

if ($bgcolor="black") $bgcolor="white";
elseif ($bgcolor="white") $bgcolor="black";




The issue is that you're doing an assignment in your if statements. You need to use the comparison operator (==) instead of the assignment (=)

Try replacing your if/elseif with this:

if ($bgcolor=="black") $bgcolor="white";
elseif ($bgcolor=="white") $bgcolor="black";