輸出一個表格
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>23ptable</title>
</head>
<body>
<?php
echo '<table border="1" width="800" align="center">';
echo '<caption>使用一個while循環輸出表格</caption>';
$i=0;
while($i<100){
if($i%10==0){
echo "<tr>";} #此處,$i%10=0,正好條件語句成立,輸出<tr>
echo "<td>".$i."</td>";
$i++; #此處,$i自加,變成了1
if($i%10==0){
echo "</tr>";} /*此處,$i=1,表格不會換行;假設在執行$i++之前,$i=9,執行后$i=10,正好換行;若把$i++放在該if后面,<tr></tr>正好成對換行,后面的循環會自成 一行,上句$i++與++$i效果一樣*/
}
echo "</table>";
?>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
還可以實現隔行換色
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>23ptable</title>
</head>
<body>
<?php
echo '<table border="1" width="800" align="center">';
echo '<caption>使用一個while循環輸出表格</caption>';
$i=0;
while($i<100){
if($i%10==0){
if($i%20==0){$bg="#fff999";}else{$bg="#333fff";} //效果如下圖
echo "<tr bgcolor=$bg>";}
echo "<td>".$i."</td>";
$i++;
if($i%10==0){
echo "</tr>";}
}
echo "</table>";
?>
</body>
</html>