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 關注
- 133 瀏覽
添加回答
舉報
