Slava Basat
Slava Basat

Reputation: 43

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

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

Upvotes: 1

Views: 23778

Answers (5)

Bobski
Bobski

Reputation: 49

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

Upvotes: 0

Slava Basat
Slava Basat

Reputation: 43

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>

Upvotes: 1

Tim Wax
Tim Wax

Reputation: 113

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

Upvotes: 1

Reshil
Reshil

Reputation: 481

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>

Upvotes: 4

Vicky Thakor
Vicky Thakor

Reputation: 3916

<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.

Upvotes: 0

Related Questions