phparrayscodeignitermodel-view-controller

Data passed from Model to Controller to View (in a CodeIgniter application) is not the expected type


I have a error on my view which I am trying to work out why but not know how to fix.

A PHP Error was encountered

Severity: Warning

Message: in_array() expects parameter 2 to be array, boolean given

What would be the best method to fix this I think it has something to do with my post permission[access] on controller that's effecting view.

View

<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>

Controller Function

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);
}

Solution

  • I belive the problem lies in your controller, like you initially said. Try this:

    In your controller, replace this:

    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();
    }
    

    with this:

    $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();
        }
    }