在Codeigniter中显示选中的复选框基本递归ID

时间:2016-07-13 09:54:12

标签: php codeigniter

我想在递归表中创建一个选中的复选框基本ID,我有这样的表

+---------+--------------+-----------+
|user_id  | function_id  | feature_id|
+---------+--------------+-----------+
| 1       |  2           |  1        |
+---------+--------------+-----------+
| 1       |  2           |  2        |
+---------+--------------+-----------+
| 1       |  2           |  3        |
+---------+--------------+-----------+

然后使用foreach

复选框
$feature = $this->db->get_where('my_table', array('function_id' => '2'))->result();
foreach ($result as $value)
{
$data = array(
        'name'          => 'feature_id',
        'value'         => $value->feature_id,
        'checked'       => // depend on feature_id if in function_id the feature id is exist then checked TRUE else FALSE
);

echo form_checkbox($data);

Plz帮助我! :(

1 个答案:

答案 0 :(得分:1)

像这样更改您的PHP代码并告诉我。

$feature = $this->db->get_where('my_table', array('function_id' => '2'))->result();

foreach ($result as $value) {

    $state = (!empty($value->feature_id)) ? TRUE : FALSE;

    $data = array( 'name' => 'feature_id',
                   'value' => $value->feature_id,
                   'checked' => $state);

    echo form_checkbox($data);
}

修改

如果您想显示所有checkboxs,那么它必须位于foreach循环中。