Alternating color rows in PHP loop

Arnold Porche Villaluz picture Arnold Porche Villaluz · Jul 17, 2011 · Viewed 17.2k times · Source

How can I have an alternating color rows in my php loop?

$num = mysql_num_rows($qPhysician);

$i=0;

while($i < $num)

{

    echo "<tr>";
    echo "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";
    echo "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";
    echo "</tr>";

    $i++;

}

I have to omit the "<" and ">" for both "tr" and "td" because it wasn't allowed in this question. :)

thanks!

Answer

Kevin Wang picture Kevin Wang · Jul 17, 2011

What do you mean? Are you saying you want to echo it in a table that alternates rows?

$num = mysql_num_rows($qPhysician);
$i=0;
echo "<table>"
while($i < $num)

{
if ($i % 2 == 0){
echo "<tr class='style1'>";
}
else{
echo "<tr class='style2'>";
}
echo "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";

echo "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";

echo "</tr>";

$i++;

}
echo "</table>";