Populate html <select> with array data from

2019-04-02 13:17发布

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

标签: php html mysqli
5条回答
男人必须洒脱
2楼-- · 2019-04-02 13:55

After the query is executed use the while loop to add the options to select

$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

查看更多
可以哭但决不认输i
3楼-- · 2019-04-02 13:57
<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 .php but it could be a problem in showing your dropdown box.

查看更多
Summer. ? 凉城
4楼-- · 2019-04-02 14:04

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>
查看更多
一纸荒年 Trace。
5楼-- · 2019-04-02 14:12

AS TIM WAX SAID THIS IS THE SOLUTION

$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>
查看更多
成全新的幸福
6楼-- · 2019-04-02 14:18

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
查看更多
登录 后发表回答