Jump to content

Get data from database in a dropdown menu


bemax

Recommended Posts

Hi, I'm trying to get data from one field in a table (database). But I get undesirable result: Here is my code ->

 

<?php

$result2 = mysql_query("SELECT DISTINCT theme FROM  mytable ")

or die(mysql_error());

 

 

while($row2 = mysql_fetch_array( $result2 ))

{

?>

<form method="post" action='<?php echo $_SERVER["PHP_SELF"]; ?>'>

 

<select name='themes'">

<?php

 

$arr= array($row2['theme']);

 

foreach($row2 as $value)

{

echo "<option value='$value'><b>". $value."</b> </option><br> ";

 

}

 

}

?>

 

The attached image file show the result that I don't wont. (It's not a dropdown).

Is there anyone who may help me, I spent a lot of time to find out but I can't.

 

Thanks a lot for your help

 

[attachment deleted by admin]

each time you loop and get the next record, you are creating a new form and a new select. the form and select should only be created once. therefore, i suggest that you move the <form> and <select> code to before the while loop. you'll also need to close the select and the form after the while loop.

hm, many other problems. i think you're trying to do this:

 

<form method="post" action=''>
<select name='themes'>
<?php
$result2 = mysql_query("SELECT DISTINCT theme FROM  mytable ") or die(mysql_error());
while($row2 = mysql_fetch_array( $result2 )) {
$value = $row2['theme'];
echo "<option value='$value'>$value</option> ";
}
?>
</select>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.