2

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?

3
  • what is the url you are checking? Commented Apr 1, 2016 at 6:34
  • Need to use that line of code to allow pages to load. Commented Apr 1, 2016 at 6:40
  • might be your query returning no any records Commented Apr 1, 2016 at 6:57

5 Answers 5

2

Can't post a comment :/

You are getting error because you are directly accessing the view file from browser address bar as:

localhost/projectname/page/view_users

Please access your controller's action something like this one:

localhost/projectname/page/getUsers

You should read about Routes as well

Sign up to request clarification or add additional context in comments.

11 Comments

Yes I will get the results in your suggested link however I do lose all the bootstrap and templates. I will only get an array on my screen containing the users information coming from the table.
You should write proper links in <link> tag as you will something like <link href= "<?php echo base_url()?>/css/style.css" > I mean to say, link properly to directory where css files are. Again insist you to read about Routes in MVC.
My website is working fine if I use localhost/projectname/view_users ... All I need is to find a way to make view_users.php to receive the $data. Any suggestion?
I do have my style links defined as you have suggested. The problem is that you suggested to access the functions in my URLs... that way I will not have access to any of my pages any more!
Try localhost/book/page/getUsers And add header view public function getUsers(){ $data= array(); $this->load->model('model_users'); $data['users'] = $this->model_users->getUsers1(); $this->load->view('templates/header'); $this->load->view('pages/view_users', $data); }
|
2

Please put this code in Controller:

$this->load->view('pages/view_users',$data);

13 Comments

Just beat me by few second!!
I already have your recommendation in my current code ... but it is not working ...
I have typed the error at the end of the question. Thanks.
@Saty but still OP didn't get solution.
@Irfana remove this one $data= array(); and try
|
1

Your code is right. But in case when your query returns empty result then here $users became NULL so it will give error on view. Put if before foreach. See below code

    <table>
     <tr>
         <td><strong>user ID</strong></td>
     </tr>
     <?php
           if(!empty($users)){ foreach ($users as $user) { ?>
     <tr>
         <td><?php echo $user->id; ?></td>
     </tr>
     <?php }}else{ ?>
       <tr>
         <td>No record found</td>
     </tr>
     <?php } ?>
 </table>

On 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 [];
        }
    }
}

8 Comments

I did the modification you suggested, now it is still giving the error plus it is showing "No record found". Any suggestion?
@Irfana check your table. is there any records?
@PathikVejani yes there is records in my table.
@Irfana change this return $query->result(); to return $query->result_array();
have you updated model too? and @PathikVejani: no need to change result to result_array
|
0

Well,

You can do this:

 public function getUsers(){

            $this->load->model('model_users');
            $users = $this->model_users->getUsers1();
            $this->load->view('pages/view_users', compact('users');
        }

Compact is more clean and If you need to change in future, will be easy.

And to access your page: localhost/your_site/getusers

But, if you want to use this standard, use Routes. Because "getusers" in URL isn't beautiful.

2 Comments

I tried it with you code and then used localhost/book/getusers but it did not work.
Error is written at the end of the question.
0

First try whether your array gives any result.

public function getUsers(){
            $this->load->model('model_users');
            $data['users'] = $this->model_users->getUsers1();
            print_r($data['users']);
        }

1 Comment

yes just checked and array is giving results and showing the table data. Problem is that the view page (view_users.php) is not receiving the $data from controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.