Member Avatar for bikertz

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></option>
            <option>Aprilia</option>
            <option>BMW</option>              
            <option>Moto Guzzi</option>
            <option>Piaggio</option>
            <option>Vespa</option>
    </select>

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.

No ducati????????;-)

<option selected="selected"><?php echo $bikes['Model']; ?></option>
Member Avatar for diafol

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>
    </select>

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.

Member Avatar for bikertz

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>
    </select>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.