错误显示在我的视图codeigniter中

时间:2015-03-09 12:33:07

标签: php arrays codeigniter

我的观点有错误,我正在尝试解决原因,但不知道如何修复。

  

遇到PHP错误

     

严重性:警告

     

消息:in_array()期望参数2为数组,布尔值为

解决此问题的最佳方法是什么?我认为它与我的帖子permission[access]在影响视图的控制器上有关。

查看

<div class="form-group">
<label class="col-sm-2 control-label">Access Permission</label>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $access)) { ?>
<input type="checkbox" name="permission[access][]" value="<?php echo $permission; ?>" checked="checked" />
<?php echo $permission; ?>
<?php } else { ?>
<input type="checkbox" name="permission[access][]" value="<?php echo $permission; ?>" />
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>

控制器功能

public function getForm() {
    $data['title'] = "Users Group";

    $this->load->model('admin/user/model_user_group');

    $id = $this->uri->segment(4);

    if (isset($id) && $this->input->server('REQUEST_METHOD') != 'POST') {
        $user_group_info = $this->model_user_group->getUserGroup($id);
    }

    $ignore = array(
        'admin',
        'dashboard',
        'filemanager',
        'login',
        'menu',
        'register',
        'online',
        'customer_total',
        'user_total',
        'chart',
        'activity',
        'logout',
        'footer',
        'header'
    );

    $data['permissions'] = array();

    $files = glob(FCPATH . 'application/modules/admin/controllers/*/*.php');

    foreach ($files as $file) {
        //$part = explode('/', dirname($file));

        $permission =  basename(strtolower($file), '.php');

        if (!in_array($permission, $ignore)) {
            $data['permissions'][] = $permission;
        }
    }

    if (null !==($this->input->post('permission[access]'))) {
        $data['access'] = $this->input->post('permission[access]');
    } elseif ($user_group_info['permission']['access']) {
        $data['access'] = $user_group_info['permission']['access'];
    } else {
        $data['access'] = array();
    }

    $this->load->view('template/user/users_group_form.tpl', $data);
}

1 个答案:

答案 0 :(得分:2)

我认为问题出在你的控制器上,就像你最初说的那样。试试这个:

在您的控制器中,将其替换为:

if (null !== $this->input->post('permission[access]')) {
    $data['access'] = $this->input->post('permission[access]');
} elseif ($user_group_info['permission']['access']) {
    $data['access'] = $user_group_info['permission']['access'];
} else {
    $data['access'] = array();
}

用这个:

$permission_access = $this->input->post('permission');
if (isset($permission_access)) {
    if (isset($permission_access['access'])) {
        $data['access'] = $permission_access['access'];
    } elseif ($user_group_info['permission']['access']) {
        $data['access'] = $user_group_info['permission']['access'];
    } else {
        $data['access'] = array();
    }
}