因此,我尝试编写一个 PHP 和 HTML 脚本,在输入限制时打印出一个表格。 假设输入了20,它会打印出1-20。 我被困在从输入打印出表格的部分。有什么建议吗?
这是我的代码...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Generator</title>
<link rel="stylesheet" href="Styles.css">
</head>
<body>
<center><h4>Prime Number Table Generator </h4>
<div id="div1">
<form action="" method="POST">
<label for="limit">Enter Limit: </label>
<input type="number" name="limit" placeholder="limit">
<br>
<input value="Submit" type="button" name="submit">
</form>
<?php
if (empty($limit)) $limit = '';
for($a = $limit; $a <= 9; $a++)
{
$b = 3;
}
if(isset($_POST['submit'])){
$column = 10;
echo "<table border='1'>";
echo "<tr>";
for ($td=1; $td<=$column; $td++) {
echo "<td>".$td. "</td>";
}
echo "</tr>";
}
?>
<div class="float_center">
<table style="width:100%">
<tr>
<center> <td colspan="10"><b>1-<?php echo $limit. ' ' ?> Prime Numbers are in RED</b></td></center>
</tr>
<tr>
<td></td>
</tr>
</table></center>
</div>
</body>
</html>
我终于打印出了 1-10,但在我为提交按钮添加脚本后,它就不再打印了。
最佳答案
我删除了你的中心标签,因为它们让我感到困惑。看看这段代码,让我知道它是否适合您!
<?php
$columns = 10;
$rows = 0;
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['limit'])) {
$rows = $_POST['limit'];
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Generator</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5" align="center">
<h4>Prime Number Table Generator </h4>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<label for="limit">Enter Limit: </label>
<input type="number" name="limit" placeholder="limit" value="<?= $rows ?>">
<br>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
<table class="table table-border mt-5">
<thead>
<tr>
<?php for ($i = 0; $i < $columns; $i++) : ?>
<td>Column - <?= $i ?></td>
<?php endfor; ?>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < $rows; $i++) : ?>
<tr>
<?php for ($x = 0; $x < $columns; $x++) : ?>
<!-- edit this section to represent your column rows -->
<td align="center">
I am a row column!
</td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</tbody>
</table>
</div>
</body>
</html>
我还添加了 bootstrap CDN,因为一切都非常困惑,而且显然我没有你的 css 文件,所以这是一种对齐事物的方法。但请随意更换它。
关于php - 无法让脚本从 PHP 的限制输入中打印出额外的列和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48797226/