使用 PHP 中来自 mysql 的数组数据填充 html <select>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18940548/
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 18:31:36  来源:igfitidea点击:

Populate html <select> with array data from mysql in PHP

phphtmlmysqli

提问by Slava Basat

I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as

我可以看到查询返回结果,但我似乎无法将它们放入 html 下拉框中。此外,下拉框的条目与查询返回的条目一样多,但它们都是空白。但是,页面源显示了正确的选项值,例如

<option value="3 John"></option>

<option value="Jude"></option>

<option value="Revelation"></option>

Can somebody help me out? Why dont they actually show in the dropdown box?

有人可以帮我吗?为什么它们实际上不显示在下拉框中?

<html>
<?php
    //Connect to the database
    $mysqli = new mysqli("localhost", "root", "", "bible");

    //Return an error if we have a connection issue
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
        }

    //Query the database for the results we want
    $query = $mysqli->query("select distinct bname as Name from kjv limit 1");

    //Create an array  of objects for each returned row
    while($array[] = $query->fetch_object());

    array_pop($array);

    //Print out the array results
    print_r($array);
    ?>
    <h3>Dropdown Demo Starts Here</h3>
    <select name="the_name">
    <?php foreach($array as $option) : ?>
        <option value="<?php echo $option->Name; ?>"></option>
    </select>
        <?php endforeach; ?>

回答by Reshil

Try This

尝试这个

<select name="the_name">
<?php foreach($array as $option) : ?>
        <option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>

回答by Tim Wax

After the query is executed use the whileloop to add the options to select

执行查询后,使用while循环添加要选择的选项

$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>

<select>
    <?php while($option = $query->fetch_object()){ ?>
        <option><?php echo $option->Name; ?></option>
    <?php } ?>
</select>

Not sure what the array_pop is doing in the code

不确定 array_pop 在代码中做什么

回答by Slava Basat

AS TIM WAX SAID THIS IS THE SOLUTION

正如 TIM WAX 所说,这就是解决方案

$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>

<select>
    <?php while($option = $query->fetch_object()){ ?>
        <option><?php echo $option->Name; ?></option>
    <?php } ?>
</select>

回答by Vicky Thakor

<select name="the_name">
<?php foreach($array as $option) : ?>
        <option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>

You ended your loop in a way that it also create <select>tag again and again. Change it and try again. I don't know much about .phpbut it could be a problem in showing your dropdown box.

您以某种方式结束了循环,它也<select>一次又一次地创建了标签。更改它并重试。我不太了解,.php但显示下拉框可能有问题。

回答by Bobski

here is mine .. im a beginner but it works for me,

这是我的……我是初学者,但它对我有用,

    $query = $mysqli->query("SELECT * FROM  `student_type_db`"); //table of student type

    echo "<select>";
    while($row = $query->fetch_array()){
         echo "<option>";
         echo $row['student_type'] . " - " . $row['student_description'];
         echo "</option>"; 
    }
    echo "</select>";

   // student type = 1 | student description = regular
   // output : 1 - regular