Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement