Yash
Yash

Reputation: 345

Retrieve stored drop-down value from database

I have a drop-down in my manageDevices View.

         <select name="TrackerType" id="dropdown" style="width:68% !important;">
         <option value="Mobile">Mobile</option>
         <option value="Device">Device</option>
         <option value="Other">Other</option>
         </select>

I want to save the value of the drop-down in the database in my addDevice view and load the editDevice view with the values retrieved from database.

I was able to save the value of the drop-down to the database.

Now I want to fetch the stored value to the editDevice page and show the selected value in the drop-down. I can get the name of the selected value using,

    <?php echo $devicearray['type'] ?>

I want to show this value as the "selected value" in the drop-down.

I am using codeigniter framework to develop this. Any hint will be highly appreciated

Upvotes: 0

Views: 985

Answers (2)

Satish Sharma
Satish Sharma

Reputation: 9635

you can try this

<select name="TrackerType" id="dropdown" style="width:68% !important;">
    <option value="Mobile" <?php echo ($devicearray['type']=='Mobile') ? "selected" : ""; ?>>Mobile</option>
    <option value="Device" <?php echo ($devicearray['type']=='Device') ? "selected" : ""; ?>>Device</option>
    <option value="Other" <?php echo ($devicearray['type']=='Other') ? "selected" : ""; ?>>Other</option>
</select>

Method:2

$$devicearray['type'] = "selected";
?>
<select name="TrackerType" id="dropdown" style="width:68% !important;">
    <option value="Mobile" <?php echo @$Mobile; ?>>Mobile</option>
    <option value="Device" <?php echo @$Device; ?>>Device</option>
    <option value="Other" <?php echo @$Other; ?>>Other</option>
</select>

Upvotes: 0

user1587985
user1587985

Reputation: 685

At your view file for each option:

 <option value="Device"<?php echo ($devicearray['type']=='Device'?'selected="selected"':''); ?>>Device</option>

It would be easier to store type as an integer, but for 3 values not big difference

Upvotes: 1

Related Questions