2 回答
TA贡献1812条经验 获得超5个赞
创建一个变量$options并在返回数据时添加<option>到变量中。
不要将所有html代码放在while.
if ($result->num_rows > 0) {
//Declare $options
$options = '';
while ($row = $result->fetch_assoc()) {
//Adding <option> to the var $options
$options .= '<option name="website_name">'. $row["website_name"]. '
</option>';
}
//HTML once, first part
$html = '<h2>Download a website</h2>
<form action="downloads.php" method="get">
<select id="website_name" name="website_name">';
//Adding <option> to the <select>
$html .= $options;
//HTML once, second part
$html .= '</select>
<input type="submit" value="Download">
</form>
<br>
<hr>
<h2>Upload to a website</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>Select file to upload:</p>
<input type="file" name="zip_file" id="fileToUpload">
<p>Select a website to upload to:</p>
<select id="website_upload_name" name="website_upload_name">'
//Adding <option> to the second <select>
$html .= $options;
//HTML once, third part
$html .= '</select>
<br>
<br>
<input type="submit" value="Upload" name="submit" style="position:relative; left: -1px;">
</form>
<br>
<hr>';
//Printing
echo $html;
}
TA贡献1943条经验 获得超7个赞
它回显的次数与找到的结果一样多的原因是因为您已将回显语句放在 while 构造中。如果您希望在 if 语句的条件得到验证时仅显示一次回显,则将回显移到 while 之外,并将选项的 html 代码放在您将在 while 内构建的变量中。在这里,我想这两个组合都需要具有与原始代码相同的选项:
if ($result->num_rows > 0) {
$options = '';
while ($row = $result->fetch_assoc()) {
$options .= '<option name="website_name">'. $row["website_name"]. '</option>';
}
echo '<h2>Download a website</h2>
<form action="downloads.php" method="get">
<select id="website_name" name="website_name">' . $options . '</select>
<input type="submit" value="Download">
</form>
<br>
<hr>
<h2>Upload to a website</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>Select file to upload:</p>
<input type="file" name="zip_file" id="fileToUpload">
<p>Select a website to upload to:</p>
<select id="website_upload_name" name="website_upload_name">' . $options . '</select>
<br>
<br>
<input type="submit" value="Upload" name="submit" style="position:relative; left: -1px;">
</form>
<br>
<hr>';
}
- 2 回答
- 0 关注
- 94 浏览
添加回答
举报