1

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

5 Answers 5

4

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>
Sign up to request clarification or add additional context in comments.

3 Comments

You are right he is printing values in attribute. @Slava Basat print it within option tag.
thank you but print what within option tag? the <select> tag?
A minor quibble but the code will work without the <?php endforeach; ?> at the end. I suspect @Reshil is carrying it over from another language like vba where it is in fact necessary to end a foreach loop with it.
1

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

2 Comments

Hello I will try this now. Array pop removes the last empty record from the query results. I saw it on youtube otherwise I would never put it there
YOU ARE A G. THIS WORKED RESPECT MAN
1

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>

Comments

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

2 Comments

You mean like this? <select name="the_name"> <?php foreach($array as $option) : ?> <option value="<?php echo $option->Name; ?>"></select></option> <?php endforeach; ?> If so, it still gives me empty data.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.