Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在postdata或Kohana中的现有用户数据中,则将复选框标记为选中_Kohana_Kohana 3 - Fatal编程技术网

如果在postdata或Kohana中的现有用户数据中,则将复选框标记为选中

如果在postdata或Kohana中的现有用户数据中,则将复选框标记为选中,kohana,kohana-3,Kohana,Kohana 3,我想标记复选框,如果它们是通过db中的现有数据或postdata中的现有数据选择的。我有一个包含所有角色的数组,$roles,并且$user\u roles包含当前角色 foreach ($roles as $r) { $checked = false; if(isset($postdata['roles'][$r->id])){ $checked = true; } else{ foreach($user_roles a

我想标记复选框,如果它们是通过db中的现有数据或postdata中的现有数据选择的。我有一个包含所有角色的数组,
$roles
,并且
$user\u roles
包含当前角色

foreach ($roles as $r) {

    $checked = false;

    if(isset($postdata['roles'][$r->id])){
        $checked = true;
    }
    else{
        foreach($user_roles as $ur){
            if($ur->id == $r->id){
                $checked = true;
            }
        }
    }

<input type="checkbox" name="roles[<?php echo $r->id; ?>]" <?php if($checked){ ?>checked="checked"<?php } ?> value="<?php echo $r->id; ?>" />
foreach($r角色){
$checked=false;
if(isset($postdata['roles'][$r->id])){
$checked=true;
}
否则{
foreach($ur用户角色){
如果($ur->id==$r->id){
$checked=true;
}
}
}

假设您正在尝试更新数据库中的现有用户

foreach($roles作为$role){
回音
表单::复选框('roles[]',$role->id,在数组中($role,$user\u roles),数组('id'=>'role-'。$role->id)),
表单::标签('role-'。$role->id,$role->name);
}
$user\u roles变量是数据库中的用户角色数组,使用
$user->roles->find\u all()
,或者是通过POST数据更新的用户角色。如果存在POST数据,则更新用户角色:

$roles=$this->request->post('roles');
foreach(ORM::factory('role')->find_all()作为$role)
{
if(在数组中($role->id,$roles))
{
//添加角色关系
$user->add('roles',新的Model_角色(数组('id'=>$Role->id));
}
其他的
{
//删除角色关系
$user->remove('roles',新的Model_角色(数组('id'=>$Role->id));
}
}
然后我仍然对视图中显示的用户角色使用
$user->roles->find_all()

这样做意味着我不必决定在视图中显示什么(POST或DB数据),因为该条件存在于模型或控制器中,并且用户角色总是最新的

$role_ids = $user_roles->as_array(NULL, 'id');

$checked = in_array($r->id, $role_ids) or Arr::path($postdata,"roles.$r->id");

echo Form::checkbox('roles['.$r->id.']', $r->id, $checked);