我遇到了一些问题 - 无法用数组中的随机值填充我的表格。有人可以帮忙吗?
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<form action="table2.php" method="POST">
<p>N: <input name = "row"; maxlength="2" size="1" value="5"></p>
<p>M: <input name = "col"; maxlength="2" size="1" value="7"></p>
<input type="submit" name="zero" value="go"/>
</form>
<?php
$array1 = array("1", "2", "3", "4", "5", "6", "7", "8");
$rand_keys = array_rand($array1, 2);
echo $array1[$rand_keys[0]] . "\n";
function draw_table($row,$col) {
$table = "<table>";
$i = 1;
do {
$table .= "<tr>";
$j = 1;
do {
$table .= "<td>X $i Y $j";
$table .= "</td>";
$j++;
}
while($j <= $col);
$table .= "</tr>";
$i++;
}
while($i <= $row);
$table .= "</table>";
return $table;
}
if(isset($_POST['zero'])) {
$rows = $_POST['row'];
$cols = $_POST['col'];
echo(draw_table($rows, $cols));
}
?>
我尝试使用array_rand,但无法理解如何修改该函数的代码
$table .= "<td>X $i Y $j";
$table .= "</td>";
$j++;
答案 0 :(得分:0)
我从未使用-r
循环,所以我希望能够简化你的生活:
do while()