How to make alternate row colours in php

The given PHP example helps you to display the row with alternate colours.

<?php
$array_data[] = array("ID", "Name", "Email");
$array_data[] = array("1", "Bill", "bill@phpmoot.com");
$array_data[] = array("2", "Jan", "jan@test.com");
$array_data[] = array("3", "Imran", "imran@phpmoot.com");
$array_data[] = array("4", "Qasim", "qasim@test.com");
$array_data[] = array("5", "phpMoot", "test@phpmoot.com");
// Header rows
print ("<table border='1' width='100%'>");
$alternate = "2"; // number of alternating rows
foreach($array_data as $key => $val){
if ($alternate == "1") {
$colour = "#66CCFF";
$alternate = "2";
}
else {
$colour = "#CCCCFF";
$alternate = "1";
}

print ("<tr bgcolor='$colour'><td>".$val[0]."</td><td>".$val[1]."</td><td>".$val[2]."</td></tr>
");
}
print ("</table>");
?>

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...