本文介紹了如何在從 MySQL 數據庫填充的 PHP 中創建動態下拉列表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試使用 PHP 和 mysql 數據庫創建一個動態下拉列表.我編寫了以下代碼并給了我輸出,但問題是它為每個項目顯示不同的下拉菜單,我希望所有項目都在一個下拉列表中.請檢查并指導我.
I am trying to create a dynamic drop down list using PHP and mysql database. I have written the following code and its giving me the output, but the problem is that its showing different drop-down menu for each item, I want all items in a single drop down list. Kindly check it and guide me.
$select_query= "Select name from category";
$select_query_run = mysql_query($select_query);
while ($select_query_array= mysql_fetch_array($select_query_run) )
{
foreach ($select_query_array as $select_query_display)
{
echo "
<select>
<option value='' >$select_query_display</option>
</select>
";
}
}
謝謝
推薦答案
擺脫內部的 foreach 循環......它對你沒有任何作用,并將開始和結束選擇標簽移到 while 循環之外.
Get rid of the inner foreach loop... it is doing nothing for you and move the start and end select tags outside of the while loop.
$select_query= "Select name from category";
$select_query_run = mysql_query($select_query);
echo "<select name='category'>";
while ($select_query_array= mysql_fetch_array($select_query_run) )
{
echo "<option value='' >".htmlspecialchars($select_query_array["name"])."</option>";
}
echo "</select>";
這篇關于如何在從 MySQL 數據庫填充的 PHP 中創建動態下拉列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!