【问题标题】:how to show the selected value from database in select box in codeigniter如何在codeigniter的选择框中显示数据库中的选定值
【发布时间】:2020-07-03 13:42:24
【问题描述】:

这里我有一个选择框,我想在其中显示存储在 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>

谁能帮帮我?

【问题讨论】:

  • 这里表格中的数据是 [{ delete:7 }] 格式的。在这里,我将显示数据库中最后插入的 id 的值。
  • 短标签可能是您的问题。你启用它们了吗?你在这里交替使用。如果您查看源 ID 猜测您的输出中有 PHP。
  • @ShwethaShetty 你有一个无效的 json 格式,集合键应该用引号括起来,比如[{"delete":7}]
  • 先生如何使用codeigniter从这个json格式中删除大括号([])

标签: php html sql json codeigniter


【解决方案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>

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

【讨论】:

    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多