I want to pass data queried in my model to the controller, to do so I am using return $data. Then in the controller I use $this->load->view('my_view', $data);
From my understanding var_dump($data);
in the view should show me the results from the query... This is not the case. I am getting "undefined variable data" and NULL from the var_dump($data);
.
Here is my model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manage_accounts_model extends CI_Model {
public function index() {
//
}
public function get_users(){
$data = array();
$data['query'] = $this->db->get('users');
return $data['query'];
}
}
Here is my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manage_accounts extends CI_Controller {
public function index() {
$this->load->view('template/header');
$this->load->model('manage_accounts_model');
$this->load->view('template/footer');
$this->load->model('manage_accounts_model');
$res = $this->manage_accounts_model->get_users();
if($res){
$this->load->view('manage_accounts_view', $data);
} else {
echo "Fail";
}
}
}
And finally my view:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
?>
<div class="container">
<h1><?php if($title){ echo $title; } ?></h1>
<?php var_dump($data['query']); ?>
</div>
You forgot it
Now access the result as $res in your code
cause
$data
is not defined in your controller tryThen get on view
Also you are sending whole query from model not result for return result you need like :-
I think you made 2 mistakes
change in your model class.
change in your controller class
in your view
Just Need to do change into controller
You did not pass the
query
key variable to view.change:
To: