user9903025
user9903025

Reputation:

php - add the selected attribute to the matching drop down options

Below drop down options are displaying from the database table columns(id, records_no). I'd like to add "selected" attribute to the options which are matching with the values of different column(matching_ids).

For Ex: From the db, id column I'm getting values 1,2,3,4,5 which are displaying in the drop down options value. Now I've a different column(matching_ids) which has values 2,3. I need to add the "selected" html attribute to the matching options in the rec_castings dropdown which has the values 2,3.

    <select class="form-control selectpicker" name="rec_castings" multiple data-live-search="true">
    <?php    
    foreach ($records_no as $records_no_list):?>
    <option value="<?php echo $records_no_list->id;?>"><?php echo $records_no_list->records_no;?> 
    </option> 
    <?php endforeach;
    ?>  
    </select>

Any help would be appreciated.

Upvotes: 0

Views: 34

Answers (1)

jaydeep
jaydeep

Reputation: 96

<select class="form-control selectpicker" name="rec_castings" multiple data-live-search="true">
    <?php    
    foreach ($records_no as $records_no_list):?>
    <option value="<?php echo $records_no_list->id;?>" <?php if($records_no_list->id==2 || $records_no_list->id==3){echo "selected";} ?> ><?php echo $records_no_list->records_no;?> 
    </option> 
    <?php endforeach;
    ?>  
    </select>

Upvotes: 1

Related Questions