本文介紹了如何在從 MySQL 數(shù)據(jù)庫填充的 PHP 中創(chuàng)建動態(tài)下拉列表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試使用 PHP 和 mysql 數(shù)據(jù)庫創(chuàng)建一個動態(tài)下拉列表.我編寫了以下代碼并給了我輸出,但問題是它為每個項目顯示不同的下拉菜單,我希望所有項目都在一個下拉列表中.請檢查并指導我.
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>
";
}
}
謝謝
推薦答案
擺脫內(nèi)部的 foreach 循環(huán)......它對你沒有任何作用,并將開始和結(jié)束選擇標簽移到 while 循環(huán)之外.
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 數(shù)據(jù)庫填充的 PHP 中創(chuàng)建動態(tài)下拉列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!