0
votes

I have a problem with user_model.php, below it's the errors:

A PHP Error was encountered Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: models/user_model.php

Line Number: 18

Backtrace:

File: \httpdocs\application\models\user_model.php Line: 18 Function: _error_handler

File: \httpdocs\application\controllers\user.php Line: 9 Function: check_role

File: \httpdocs\index.php Line: 292 Function: require_once

user_model.php

public function check_role()
{
    $user_id = $this->session->userdata('admin_user_id');
    // get roles
    if ($user_id) {
        $row = $this->db->get_where(TBL_USERS, array('id' => $user_id))->row();
        $roles = $this->db->get_where(TBL_ROLES, array('id' => $row->role_id))->row_array();
        foreach ($roles as $key => $value) {
            $this->session->set_userdata($key, $value);
        }
    }
}

what is wrong with foreach?

1
var_dump($roles) and see what you are getting. - Sougata Bose
what you are trying to assign in session? - Abdulla Nilam
@b0s3 where to put the var_dump($roles)... - Viruzzz
After $roles = $this->db....... - Sougata Bose
it show me the text: NULL - Viruzzz

1 Answers

0
votes

Try to check count($roles) before foreach().

public function check_role()
{
    $user_id = $this->session->userdata('admin_user_id');
    // get roles
    if ($user_id) {
        $row = $this->db->get_where(TBL_USERS, array('id' => $user_id))->row();
        $roles = $this->db->get_where(TBL_ROLES, array('id' => $row->role_id))->row_array();
        if(count($roles)>0)
        {
            foreach ($roles as $key => $value) {
               $this->session->set_userdata($key, $value);
            }
        }
    }
}