如何在从 MySQL 数据库填充的 PHP 中创建动态下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17691936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:15:56  来源:igfitidea点击:

How to Create a Dynamic Drop Down List in PHP populated from MySQL Database

phphtmlmysqldynamicdrop-down-menu

提问by Taha Kirmani

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.

我正在尝试使用 PHP 和 mysql 数据库创建一个动态下拉列表。我编写了以下代码并给了我输出,但问题是它为每个项目显示不同的下拉菜单,我希望所有项目都在一个下拉列表中。请检查它并指导我。

        $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>
                ";


                }

            }

Thanks

谢谢

回答by Orangepill

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.

摆脱内部 foreach 循环......它对你没有任何作用,并将开始和结束选择标签移到 while 循环之外。

$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>";

回答by Amir Habibzadeh

take look at this code.

看看这段代码。

$select_query=          "Select name from category";
$select_query_run =     mysql_query($select_query);

echo "<select>";
while   ($select_query_array=   mysql_fetch_array($select_query_run) )
{
        echo "<option value='' >".$select_query_array['name']."</option>";                        
}
echo "</select>";

回答by Aryan Arora

<?php

$res = mysqli_query($conn, "SELECT DISTINCT coloumn_name FROM table_name;" );
while($row = mysqli_fetch_array($res))    
{
    echo "<option value='" . $row['selected_coloumn']. "'>" . $row['selected_coloumn'] . "</option>";
}
?>

In this example please select your 'coloumn_name'and 'table_name'.

在此示例中,请选择您的'coloumn_name''table_name'

回答by vjy tiwari

You may try it

你可以试试

    $select_query=          "Select name from category";
    $select_query_run =     mysql_query($select_query);
    $select_query_array=   mysql_fetch_array($select_query_run)
    $select = "<select>";
    foreach ($select_query_array as $val)
    {
        $select .= "<option value='".$val['name']."' >".$val['name']."</option>"; 


    }

    $select = "</select>";

    echo $select;

Hope It will work

希望它会起作用

回答by Yogesh

$select_query= "Select name from category";
$select_query_run =     mysql_query($select_query);
$selectTag = "<select>";
while   ($select_query_array=   mysql_fetch_array($select_query_run) ){
    foreach ($select_query_array as $select_query_display){
        $selectTag .="<option value='' >$select_query_display</option>";
    }
}
$selectTag .= "</select>";

   echo $selectTag;