Search code examples
phphtmldrop-down-menusmarty

How to display selected item at drop down list?


I am currently working on a edit form with drop down list. I have a edit form that display current item from database. But I have no idea how my drop down menu to display current value from database that match drop down list. I know my explanation a bit confuse therefore I picture below will show better explanation.

enter image description here

This is one of my edit form that showing drop down menu. What I want is display database value with this view of drop down menu.

Example :

My database value is Pending. Then it will show Pending inside this view of drop down menu. And when I click on the drop down menu, it will show me like below picture :

enter image description here

this is what I am looking for. And below is the code that I tried.

<div class="floatleft">
    <select name="select2" class="select-form-standard" >
         <option value="0" id="0" name="status">Deleted</option>
         <option value="1" id="1" name="status">Active</option>
         <option value="2" id="2" name="status">Pending</option>
         <option value="2" id="2" name="status">Suspended</option>
    </select>
</div>

But I just know how to design a drop down menu and do not have idea how to work like this. I am working with smarty framework. Can someone give me some solution to work on it? Thanks in advanced.


Solution

  • Add selected attribute for the option which must be selected. Assume $value = 0

    <?php $value = 0; ?>
    <select name="select2" class="select-form-standard" >
        <?php foreach($options as $id => $option) { ?>
            <option value="<?php echo $id ?>"<?php 
                if($id == $value) { 
                echo " selected"; 
                } ?>><?php echo $option ?></option>
        <?php } ?>
    </select>