phpwhile-looprowsalternating

Alternating color rows in PHP loop


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!


Solution

  • 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>";