Users.php Controller
class Users extends CI_Controller{
public function userList(){
$data['results'] = $this->user_model->get_users();
$this->load->view('users_view',$data);
}
}
user_model.php Model
class User_model extends CI_Model{
public function get_users()
{
$query = $this->db->get('users');
return $query->result();
}
}
users_view.php View
foreach ($results as $list) {
echo $list->id;
}
main.php View
$this->load->view('users_view');
When i view http://localhost:81/rokki/users/userlist the id is displayed. But, when i view in http://localhost:81/rokki/home thru the main.php I'm getting errors as below :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: results
Filename: views/users_view.php
Line Number: 41
Backtrace:
File: C:\xampp\htdocsokki\application\views\users_view.php Line: 41 Function: _error_handler
File: C:\xampp\htdocsokki\application\views\layouts\main.php Line: 20 Function: view
File: C:\xampp\htdocsokki\application\controllers\Home.php Line: 7 Function: view
File: C:\xampp\htdocsokki\index.php Line: 315 Function: require_once
I'm newbie to the CI. Please help me. Thanks in advance
Do you have a controller for home
like you do for users
? Looking at your call stack it appears you have one so I assume it's something like this if it isn't already.
class Home extends CI_Controller{
public function index(){
$data['results'] = $this->user_model->get_users();
$this->load->view('main',$data);
}
}
Then in your main.php passing the results data to the view_users view like so.
$this->load->view('view_users', $results);
Try
Because your function userList
http://localhost:81/rokki/users/userList
Users.php
class Users extends CI_Controller{
public function userList(){
$this->load->model('user_model');
$data['results'] = $this->user_model->get_users();
$this->load->view('users_view',$data);
}
}
User_model.php
class User_model extends CI_Model{
public function get_users()
{
$query = $this->db->get('users');
// Change the result to array
return $query->result_array();
}
}
On the view
foreach ($results as $list) {
echo $list['id'];
}