Alternating Table Row Colours using PHP

This is row: 0

This is row: 1

This is row: 2

This is row: 3

This is row: 4

This is row: 5

This is row: 6

This is row: 7

This is row: 8

This is row: 9

The Code

In the <style> block:
tr.grey { background-color: #cccccc; }
tr.white { background-color: #ffffff; }

... and to create the table using PHP:
<table>
<?PHP

$rows=10;
$rowcolour = array("grey", "white");
$rowswitch = 0;
$index = 0;

while($index < $rows) {
    print("<tr class=\"$rowcolour[$rowswitch]\">");
    print("<td><p>This is row: $index</p></td>");
    print("</tr>");
    $rowswitch = !$rowswitch;
    $index ++;
}
?>
</table>


Email me | Nigenet Home