【问题标题】:getting difficulty in generating the Dyanmic checkbox from the database从数据库生成动态复选框时遇到困难
【发布时间】:2012-05-31 02:21:15
【问题描述】:

我正在使用 Codeigniter 和 Datamapper 进行数据库查询。我的控制器如下

function fetch_interested_in()
{
    $in = new Interested_in();      
    $in -> get();
    $interested_in = array();
    foreach($in -> all as $data)
    {
        $interested_in[$data -> in_id] = $data -> in_title;
    }
    return $interested_in;
}

而我的视图文件如下

    <?php foreach($interested_in as $in)

                        echo form_checkbox('in_in[]', $in -> in_id); 

    ?>

我的表中有 3 行名为 Interested_in。有 2 列,名称是 in_id 和 in_title。当我运行代码时,我在页面中的 3 个位置一个接一个地收到以下错误。

**

遇到 PHP 错误 严重性:通知消息:正在尝试获取 非对象文件名的属性:views/poverview.php 行号:137

**

请让我知道我哪里出错了。我真的会非常感谢你。提前致谢。

【问题讨论】:

    标签: php mysql codeigniter datamapper codeigniter-datamapper


    【解决方案1】:
    <?php foreach($interested_in as $in)
    
                        echo form_checkbox('in_in[]', $in); 
    
    ?>
    

    够了。

    您的标题/ID 已“保存”在 $in 而不是 $in-&gt;id_id

    但在你的情况下,我猜你想要:

    <?php foreach(array_keys($interested_in) as $id):
    
          echo '<label>'.$interested_in[$id].'</label>';
          echo form_checkbox('in_in[]', $id); 
    
          endforeach;
    ?>
    

    【讨论】:

    • 哦哇...它的工作...非常感谢您的快速回复...还有一个小疑问。我也想显示标题...我要写什么...?列名是 in_title... 在此先感谢...
    • 完全正确。我无法以正确的方式提出我的问题。对此感到抱歉。你说的对。但是上面的代码显示了 3 次 lebel 和 1 次的复选框。
    • 我想在标签旁边显示复选框。再次感谢您的快速回复。
    • 你真是太棒了。我不能这么懒惰。你太棒了。它像魔术一样工作......谢谢谢谢谢谢谢谢非常感谢......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2016-08-11
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多