Alternating table row colors : Array Index « Data Structure « PHP






Alternating table row colors

 
<?
$row_color = array('red','green');
$color_index = 0;
$meal = array('breakfast' => 'A',
              'lunch' => 'B',
              'snack' => 'C',
              'dinner' => 'D');
print "<table>\n";

foreach ($meal as $key => $value) {
    print '<tr bgcolor="' . $row_color[$color_index] . '">';
    print "<td>$key</td><td>$value</td></tr>\n";

    $color_index = 1 - $color_index;
}
print '</table>';
?>
  
  








Related examples in the same category

1.Adding elements with []
2.Setting an Array's Size
3.Getting array size
4.Loops through an indexed array of fruit names and creates a new array with the count of each fruit name.