I am reading data from a mysql database and displaying it on a web page for the user to make changes. One of the values that I am getting from the database is to be displayed in a Select tag:

    Manufacturer:   <select name="Manufacturer" tabindex="1" >
            <option>Moto Guzzi</option>

This value is in the php variable $bikes['Model'] which would have one of the above options.

How would I get my page to make the value of $bikes['Model'] to be selected in the Manufacturer Select option above?

Thank you for any help that I may receive.

4 Years
Discussion Span
Last Post by bikertz

Few ways. The long way:

<?php if(!isset($bikes['Model']))$bikes['Model'] == '';?>
 Manufacturer:   <select name="Manufacturer" tabindex="1" >
            <option<?php if($bikes['Model'] == '')echo ' selected="selected"';?>></option>
            <option<?php if($bikes['Model'] == 'Aprilia')echo ' selected="selected"';?>>Aprilia</option>
            <option<?php if($bikes['Model'] == 'BMW')echo ' selected="selected"';?>>BMW</option>              
            <option<?php if($bikes['Model'] == 'Moto Guzzi')echo ' selected="selected"';?>>Moto Guzzi</option>
            <option<?php if($bikes['Model'] == 'Piaggio')echo ' selected="selected"';?>>Piaggio</option>
            <option<?php if($bikes['Model'] == 'Vespa')echo ' selected="selected"';?>>Vespa</option>

Not advised, unless the dropdown has to be a static affair.
I'd actually create the select with values from a DB or array:

$selectbikes = array("","Aprilia","BMW","Moto Guzzi","Piaggio","Vespa");
//alternatively get the above from a DB query and use a while loop

if(!isset($bikes['Model']))$bikes['Model'] == '';
$options = '';

foreach($selectbikes as $bike){
    $sel = ($bike == $bikes['Model']) ? ' selected = "selected"' : ''; 
    $options .= "\n\t<option value='$bike'$sel>$bike</option>";

$select = "<select name='Manufacturer' tabindex='1'>$option\n</select>";

Usually you'd have a value as an integer in the select - relating to it's 'id' in a DB table, e.g.

<option value="7">Piaggio</option>

You also store the 'id' value in the $bikes['Model'] and compare those.


Thanks for the help. I used the following code to solve the problem, as suggested by diafol:

Manufacturer:   <select name="Manufacturer" tabindex="1" >
            <option value=""<?php if($bikes['Manufacturer'] == '') echo 'selected="selected"';?>></option>
            <option value="Aprilia"<?php if($bikes['Manufacturer'] == 'Aprilia') echo 'selected="selected"';?>>Aprilia</option>
            <option value="BMW"<?php if($bikes['Manufacturer'] == 'BMW') echo 'selected="selected"';?>>BMW</option>                
            <option value="Moto Guzzi"<?php if($bikes['Manufacturer'] == 'Moto Guzzi') echo 'selected="selected"';?>>Moto Guzzi</option>
      <option value="Piaggio"<?php if($bikes['Manufacturer'] == 'Piaggio') echo 'selected="selected"';?>>Piaggio</option>
            <option value="Vespa"<?php if($bikes['Manufacturer'] == 'Vespa') echo 'selected="selected"';?>>Vespa</option>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.