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

后端 未结 5 418
一个人的身影
一个人的身影 2020-12-12 07:01

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 show

相关标签:
5条回答
  • 2020-12-12 07:21
    <?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'.

    0 讨论(0)
  • 2020-12-12 07:24

    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

    0 讨论(0)
  • 2020-12-12 07:35
    $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;
    
    0 讨论(0)
  • 2020-12-12 07:37

    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>";
    
    0 讨论(0)
  • 2020-12-12 07:45

    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>";
    
    0 讨论(0)
提交回复
热议问题