根据复选框在生成的表中的值更改复选框状态


Change checkbox state according to its value in generated table

这是我生成表的代码:

foreach($list_user as $temp){
        $this->table->add_row(++$i,
        $temp->ID_user,
        $temp->nama,            
        $temp->email,
        form_checkbox('selected[]', $temp->active),
        anchor('user/update/'.$temp->ID_user, 'update', array('class'=>'update')).' '.
        anchor('user/delete/'.$temp->ID_user, 'delete', array('class'=>'delete', 
        'onclick' => "return confirm('Apa Anda yakin mau menghapus data user $temp->nama?')"))
        );
    }

现在,如果我在没有复选框的情况下生成$temp->active,它会给我一个正确的值,比如1(活动)或0(不活动)我的问题是:如何更改复选框的值,以便如果值为1,它将被自动选中,反之亦然。感谢

使用form_checkbox()的第三个布尔参数:

form_checkbox('selected[]', $temp->active, $temp->active)

form_checkbox()函数有以下参数:

form_checkbox('nameofcheckbox', 'value', BOOL);

所以代码

form_checkbox('selected[]', '123', $temp->active);

将产生

<input type="checkbox" name="selected[]" value="123" checked="checked" />

如果$temp->active值为1