New to Codeigniter framework. I am trying to simply read user information from the database and show the result in a table in the view. (I have 6 pages in my website that in each page I want to show some user data)
default Controller:
class Pages extends CI_Controller {
public function view($page = 'home') {
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('pages/' . $page);
}
public function getUsers(){
$data= array();
$this->load->model('model_users');
$data['users'] = $this->model_users->getUsers1();
$this->load->view('pages/view_users', $data);
}
}
Model:
class Model_users extends CI_Model {
function __construct() {
parent::__construct();
}
function getUsers1(){
$query = $this->db->query('SELECT * FROM user');
if($query->num_rows()>0){
return $query->result();
} else {
return NULL;
}
}
}
View:
<table>
<tr>
<td><strong>user ID</strong></td>
</tr>
<?php foreach ($users as $user) { ?>
<tr>
<td><?php echo $user->id; ?></td>
</tr>
<?php } ?>
</table>
Error:
Severity: Notice
Message: Undefined variable: users
Filename: pages/view_users.php
Any suggestion on why this error occurs and how to solve it?