填充html< select>在PHP中使用来自mysql的数组数据 [英] Populate html <select> with array data from mysql in PHP

查看:82
本文介绍了填充html< select>在PHP中使用来自mysql的数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


推荐答案

试试这个

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

这篇关于填充html&lt; select&gt;在PHP中使用来自mysql的数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆