0

Controller:

$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('Login_model');

if($this->Login_model->login_user($username, $password)) {
        $data['user_data'] = $this->Login_model->login_user($username, $password);
        echo $user_data->fname;
        //$user_array = array(
        //'fname' => $user_data->fname,
        //'level' => $user_data->level,
        //'logged_in' => true
        //);

    //$this->session->set_userdata($user_array);
    redirect ('home');

    } else {

        $this->load->view('inc/login_header');
        $this->load->view('login');
        $this->load->view('inc/login_footer');

    }

Model:

public function login_user($username, $password) //This function returns an array
{
    $this->db->where('username', $username);
    $this->db->where('password', $password);
    $query = $this->db->get('logins'); //create query

    if($query->num_rows() == 1) {
        //return $query->result_array();
        return $query->row();
    } else {
        return false;       
    }
}

I keep geting this error: Message: Undefined variable: user_data Filename: controllers/Login.php

This error is referencing the line: echo $user_data->fname;

In my mind I am trying to create an array called user_data from the data in the row from the model.

Thanks in advance for your help.

  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Spoody Apr 28 '18 at 17:51
  • It complains as you don't get `$user_data` from anywhere. Have you mixed up the naming of `$data['user_data']` in the line above? – JimL Apr 28 '18 at 17:51
  • I have tried mixing it up with the same results. I am wondering if I am creating the array wrong. – phishyman2 Apr 28 '18 at 17:54

1 Answers1

0

Use this :

$data['user_data']->fname;

instead of this :

echo $user_data->fname;

Or Like this :

$user_data = $this->Login_model->login_user($username, $password);
echo $user_data->fname;

should be like this :

if($this->Login_model->login_user($username, $password)) {
    $data['user_data'] = $this->Login_model->login_user($username, $password);
    echo $data['user_data']->fname;
    //$user_array = array(
    //'fname' => $user_data->fname,
    //'level' => $user_data->level,
    //'logged_in' => true
    //);

//$this->session->set_userdata($user_array);
redirect ('home');

} 
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • That worked. I then changed it to this to make it cleaner: $data = $this->Login_model->login_user($username, $password); echo $data->fname; – phishyman2 Apr 28 '18 at 17:58