0

我正在尝试使用 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>
                ";


                }

            }

谢谢

4

5 回答 5

4

摆脱内部 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>";
于 2013-07-17T05:45:45.897 回答
1

看看这段代码。

$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>";
于 2013-07-17T05:46:21.663 回答
1
<?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>";
}
?>

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

于 2017-08-01T05:41:18.253 回答
0
$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;
于 2013-07-17T05:43:50.570 回答
0

你可以试试

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

希望它会起作用

于 2017-08-01T05:51:01.537 回答