useruser
useruser

Reputation: 159

PHP Error - Cannot use a scalar value as an array

Hello I am a beginner in codeigniter framework and I have some problems.

On my index page i have login form, and when user insert username and password and clicks submit button he calls controller's method login whose task is to collect username and password from form and to pass the data to models method "login($username, $password)" and then models method returns true or false depending on whether we have valid user in database or not, and then controller's method either pass the user further or returns him on the beginning. My problem is that i want to know users access level, and i made this method in model

public function users_level($username){

    $query = $this->db->get_where('users',array('username' => $username) );

    $users_level = $query->result();    // I tried with these too $users_level[] = $query->result();    

    return $users_level;
}

with which i want to return user's access level, and to use that information in order to determine what view to present for that specific user.

This is login method from controller:

public function login(){
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->load->model('model_user');
        $result = $this->model_user->login($username,$password);
        if ($result == true){     
                $result['level'] = $this->model_user->users_level($username);    //i believe that mistake is maybe in this line
                $this->welcome();   //I'm going to send information to welcome method about users level of access                  
            }else if ($result == false){
                $this->index();
            }
}

And this is an error that occurs

Severity: Warning

Message: Cannot use a scalar value as an array

Filename: controllers/controler_user.php

Line Number: 33

Backtrace:

File: C:\wamp\www\ci_project_2015\application\controllers\controler_user.php
Line: 33
Function: _error_handler

File: C:\wamp\www\ci_project_2015\index.php
Line: 292
Function: require_once

Upvotes: 1

Views: 12405

Answers (1)

topher
topher

Reputation: 14860

You are trying to use $result as an array when it is already initialized as a boolean. You could reinitialize it as an array using:

$result = array('level' => $this->model_user->users_level($username));

or

$result = array();
$result['level'] = $this->model_user->users_level($username);

However, this is a bad idea since you are using the same variable for different things. A better solution is to rename one of the variables e.g.

$logged_in = $this->model_user->login($username,$password);
if ($logged_in == true){

Or better yet, since the boolean is only being used once, you can skip the initialization of $logged_in and use the result of $this->model_user->login($username,$password) directly in the condition

if ($this->model_user->login($username,$password)) {

You can omit the == true since it returns a boolean.

Upvotes: 3

Related Questions