Search code examples
phpmysqlsqlcodeignitermodels

undefined variable when Adding Dynamic Data to the View from database query in codeigniter


I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in. For that i've created a model as follows,

public function get_last_ten_entries()
      {
              $query = $this->db->query("SELECT username FROM public");
              return $query->result();
      }

and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,

<h1><?php echo $data;?></h1>

through controller,

  $this->load->model('loginmodel');
          $login_id = $this->loginmodel->login_valid($username, $password);
            if($login_id){
              $this->load->library('session');

              $this->session->set_userdata('user_id','$login_id');


              $data['query'] = $this->loginmodel->get_last_ten_entries();

              $this->load->view('admin/account', $data);


            }

but when i execute this i get,

A PHP Error was encountered Severity: Notice

Message: Undefined variable: data

Filename: admin/account.php

Line Number: 11


Solution

  • Just use $query instead of $data in the view