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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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