01<?php 02 03// just some random data in an array 04$data=array('this','is','just','an','example','to','show','the','alternating','row','colors'); 05$rows=count($data); 06 07// the two colors to switch between 08$rowcolor1='#F0F0F2'; 09$rowcolor2='#FFFFFF'; 10 11// the background colors on mouseover 12$hovercolor1='#BAD4EB'; 13$hovercolor2='#DCE9F4'; 14 15echo' 16<table style="caption-side: top; 17 border: 0.1em solid #eee; 18 border-collapse: collapse; 19 margin: 1em; 20 width: 30em;"> 21 <caption style="font-weight: bold;">Demonstration of alternate row colors</caption>'; 22 23for($n=0;$n<$rows;$n++) 24{ 25// this is where the magic happens 26if($n%2==1) 27{ 28// add more things to swop with each cycle 29$style=$rowcolor1; 30$hoverstyle=$hovercolor1; 31}else{ 32$style=$rowcolor2; 33$hoverstyle=$hovercolor2; 34} 35 36echo' 37 <tr id="row'.$n.'" style="background:'.$style.';" 38 onmouseover="this.style.background=\''.$hoverstyle.'\'" 39 onmouseout="this.style.background=\''.$style.'\'"> 40 <td style="padding: 0.3em 1em;">'.$data[$n].'</td> 41 </tr>'; 42} 43 44echo' 45</table>'; 46 47?>