1. 程式人生 > >【PHP】簡單的隔行顏色區分表格的實現

【PHP】簡單的隔行顏色區分表格的實現

<html>
	<head>
		<title>Creating tables</title>
		<meta charset="utf-8" />
	</head>
	<body>
		<table border="1" style = 'border-collapse: collapse;'align="center">
			<caption><h1>表格示意圖</h1></caption>
			<tr>
				<th>學號</th>
				<th>年級</th>
				<th>姓名</th>
				<th>性別</th>
				<th>政治面貌</th>
				<th>年齡</th>
				<th>成績</th>
				<th>等級</th>
				<th>獎學金</th>
				<th>有無違紀</th>
			</tr>
				<?php
					$i =0;
					while($i<20){					//第一層迴圈,將第一行迴圈20
						if($i%2==0){//對資料行進行判斷,當為偶數行時,灰色賦給$bgcolor
							$bgcolor = 'BBBBBB';
						}else{
							$bgcolor = 'FFFFFF';
						}
						
						$i++;        
						echo "<tr>";
						$j=0;
						while ($j<10){				//第二層迴圈,得到10個數據
							$j++;
							echo "<td style = 'text-align :center' bgcolor = $bgcolor>資料</td>";
						}	
						echo "</tr>";
					}
				?>
		</table>
	</body>
</html>

效果圖: