1

I want to populate a drop down list with values from database.

<?php
require 'conn.php';

$filter=mysql_query("select distinct fuel_type from car");
while($row = mysql_fetch_array($filter)) {
$options ="<option>" . $row['fuel_type'] . "</option>";

$menu="<form id='filter' name='filter' method='post' action=''>
  <p><label>Filter</label></p>
    <select name='filter' id='filter'>
      " . $options . "
    </select>
</form>";

echo $menu;
}
?>

The issue is that I get two lists instead of one list with the values inside. Please advise

drop down list

3 Answers 3

2

You need to remove the $menu from your loop:

<?php
require 'conn.php';

$options = '';
$filter=mysql_query("select distinct fuel_type from car");
while($row = mysql_fetch_array($filter)) {
    $options .="<option>" . $row['fuel_type'] . "</option>";
}

$menu="<form id='filter' name='filter' method='post' action=''>
  <p><label>Filter</label></p>
    <select name='filter' id='filter'>
      " . $options . "
    </select>
</form>";

echo $menu;

?>
Sign up to request clarification or add additional context in comments.

1 Comment

You're probably missing the . in the while loop. Copy and paste my code and try again.
2

echo $menu; should be outside your loop.

Comments

1
<?php
require 'conn.php';

$filter=mysql_query("select distinct fuel_type from car");
$menu="
<form id='filter' name='filter' method='post' action=''>
  <p><label>Filter</label></p>
    <select name='filter' id='filter'>";

// Add options to the drop down
while($row = mysql_fetch_array($filter))
{
  $menu .="<option>" . $row['fuel_type'] . "</option>";
}

// Close menu form
$menu = "</select></form>";

// Output it
echo $menu;
?>

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.