1. 前端页面index.php
<form action="test.php" method="post">
<b>输入行列生成表格</b><br><br>
输入行:<input type="text" name="cols"><br><br>
输入列:<input type="text" name="rows"><br><br>
<input type="submit" value="生成表格">
<input type="reset" value="重置行列">
</form>
2. 逻辑
<?php
if(!empty($_POST['cols'])){ // 判断用户输入不为空
echo"<b>用户动态输出表格行".$_POST['cols'].",列".$_POST['rows'].
"</b></>";
$color=""; // 隔行变色颜色变量
echo "<table border='1' width='200px' height='200px' align='center'
cellspacing='0'>";
for($i = 0 ; $i < $_POST['cols'] ; $i++){ // 行循环的条件范围判断设置为变量
if($i%2==0){ // 通过奇、偶行来进行判断
$color="red"; // 偶数行为红色
}else{
$color="blue"; // 奇数行为蓝色
}
echo "<tr bgcolor='".$color."'>"; // 使用颜色变量
for($j = 0 ;$j < $_POST['rows'] ; $j++){// 单元格循环的条件范围判断设置为变量
echo "<td>".$j."</td>";
}
echo "</tr>";
}
echo"</table>";
}
?>