Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdown list from mysql database

Status
Not open for further replies.

yebaws

Technical User
Mar 22, 2007
42
GB
I want to set an "initially selected" option for a drop down list to be the same as the data in a field from a database query. For example:

Code:
<select name="fruit" id="fruit">
      <option value="apples">apples</option>
	  <option value="pears">pears</option>
</select>

If I query the database and the value returned for the "fruit" field is "apples", then I want to write a drop down menu like this:

Code:
<select name="fruit" id="fruit">
      <option value="apples" selected>apples</option>
	  <option value="pears">pears</option>
</select>

So that you can see from the drop down what value is in the database, and change it if you want.

Any ideas?

Thanks.
 
You'll need to dynamically create your drop dowwn
i.e:

Code:
<?PHP
$output.="";

$output.="<option value=apples ";

if($value_from_db=='apples'){
$output.="selected=selected>";
}

$output.="Apples</option>";

$output.="<option value=pears ";
if($value_from_db=='pears'){
$output.="selected=selected>";
}

$output.="Pears</option>";

...

?>




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you. That does the job.

I was hoping there might be a slightly less long winded way to do it as I have a bit more than apples and pears, but I guess not.
 
If you are generating the drop down from something such as a database or even an array, you can just loop through the list of options and have a single if statement to generate the selected item.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i use this method of generating options

Code:
function getOptions($tableName, $value, $display, $where = '', $selected=''){
 $options = array();
 $where = empty($where) ? '' : 'where $where';
 $sql = "select $value, $display from $tableName $where";
 $result = mysql_query($sql);
 while ($row = mysql_fetch_array($result)){
  $_selected = ($row[0] == $selected) ? 'selected="selected" :'';
  $options[] = <<<HTML
 <option value="{$row[0]}" $_selected>{$row[1]}</option>
HTML;
 } //end while
 return implode("\r\n", $options);
}

and to use the function in your code
Code:
echo "<select name=\"somefield\">";
echo getOptions('fruits', 'id', 'fruitName');
echo "</select>";

you can use the fourth parameter to filter the options and fifth to make forms sticky.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top