如何在codeigniter的选择框中显示从数据库中选择的值

时间:2020-03-23 03:48:53

标签: php html sql json codeigniter

在这里,我有一个选择框,我要在其中显示以JSON格式存储在数据库中的值。如果存在该值,则显示所选值,否则显示默认选项Delete leads选项。它无法正常工作。

<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>

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我认为您可以像这样将$slctoptn['delete']更改为$slctoptn[0]['delete']变量:

<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>

这将使用'delete'父数组内唯一的$slctoptn数组。