简体   繁体   中英

how to show the selected value from database in select box in codeigniter

Here I have a select box where I want to show the value which is stored in the database that is in the JSON format. If the value is present, it shows the selected value, otherwise it shows the default option Delete leads option. It's not working properly.

<div class="col-md-7">
    <select class="form-control" id="spm" name="spm" required style="">>
        <option value=""> Delete Leads </option>
        <? 
            foreach($slct_optn as $slct_optns)
            {
                $slctoptn = json_decode($slct_optns['spam_management'],1);
                ?>
                <option value="7" <?php if($slctoptn['delete']==7) {?> selected="selected" <? } ?>>1 Week Older</option>
                <option value="30" <?php if($slctoptn['delete']==30) {?> selected="selected" <? } ?>>1 Month</option>
                <option value="60" <?php if($slctoptn['delete']==60) {?> selected="selected" <? } ?>>2 Month</option>
            <? }
        ?>
    </select>

Can anyone please help me?

I think you could change the $slctoptn['delete'] to $slctoptn[0]['delete'] variable like this :

<div class="col-md-7">
    <select class="form-control" id="spm" name="spm" required style="">>
        <option value=""> Delete Leads </option>
        <? 
            foreach($slct_optn as $slct_optns)
            {
                $slctoptn = json_decode($slct_optns['spam_management'],1);
                ?>
                <option value="7" <?php if($slctoptn[0]['delete']==7) {?> selected="selected" <? } ?>>1 Week Older</option>
                <option value="30" <?php if($slctoptn[0]['delete']==30) {?> selected="selected" <? } ?>>1 Month</option>
                <option value="60" <?php if($slctoptn[0]['delete']==60) {?> selected="selected" <? } ?>>2 Month</option>
            <? }
        ?>
    </select>

This will use the only 'delete' array inside the $slctoptn parent array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM