WTFZane
WTFZane

Reputation: 612

PHP - Dropdown menu(select) not working properly to while loop

I want my dropdown-textbox-select(I'm confused what it really is) to query all the data in the appropriate table I want to display. But it is displaying all the data with separated dropdown-textbox-select. I just want the data to be inside. Here's what I'm doing so far.

<?php   


    $sel_admin = "SELECT * FROM author";
    $rs_admin = mysql_query($sel_admin);
    while($row = mysql_fetch_array($rs_admin))
                {
                echo '<select class="form-control">';
                echo"  <option value='volvo'>" . $row['author_firstname'] . $row['author_lastname'] ."</option>";
                echo'</select>';
                }
                ?>

PS. If the user click submit, I want the author_id to be save, instead of the name. How can this be also?

Upvotes: 0

Views: 1409

Answers (1)

user3849602
user3849602

Reputation:

Your select tag should be outside of the while loop : you want only one select field, containing options with all the fetched rows.

echo '<select class="form-control">';
while($row = mysql_fetch_array($rs_admin))
{
    echo"  <option value='volvo'>" . $row['author_firstname'] . $row['author_lastname'] ."</option>";
}
echo'</select>';

If you want to get the author_id from this form, just put this value in the option's value attribute.

echo"  <option value='". $row['author_id'] ."'>" . $row['author_firstname'] . $row['author_lastname'] ."</option>";

Don't forget to give a name to your select tag.

Upvotes: 2

Related Questions