Search code examples
phpmysqldatabasecodeignitershow

Codeigniter Message: Undefined variable: query


SOLVED!

thanks to Fabio i realized that i missed something in my index function of my controller.


i am working on a view that show's me the data from the database.

Now i get the error: Message: Undefined variable: query.

my Model:

<?php
class categorieen_model extends CI_Model{

    function categorieen_getall()
    {
        $this->load->database();
        $query = $this->db->get('Categorieen');

        return $query->result();
    }
}

?>

My controller

public function get_All()
{
    $this->load->model('categorieen_model');
    $data['query'] =
    $this->categorieen_model->categorieen_getall();

    $this->load->view('sidebar', $data);
}

My view

<ul>
<?php foreach ($query as $row): ?>
    <?php echo $row->Categorie; ?>
<?php endforeach; ?>
</ul>

Last week i tried this with the same database, another table and other data and names in my controllers/views and it worked. when i changed it today to my new table it did not work anymore.

Please help

Thanks


Solution

  • You're not passing any data on the index method to the views

    <?php
      class Home extends CI_Controller{
    
        public function index() {
          //add this code
          $this->load->model('Categorieen_Model');
          $data['query'] = $this->Categorieen_Model->categorieen_getall();
          $this->load->vars($data);
          //end of new code
          $this->load->view('header');
          $this->load->view('navmenu');
          $this->load->view('sidebar');
          $this->load->view('home');
          $this->load->view('sidebar2');
          $this->load->view('sidebar3');
          $this->load->view('footer');
        }
    
        public function get_All(){
          $this->load->model('Categorieen_Model');
          $data['query'] = $this->Categorieen_Model->categorieen_getall();
    
          $this->load->view('sidebar', $data);
        }
    
      }
    ?>