0

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

9
  • Does the query return any rows. Try doing a print_r($query->result()); before you return the results; Commented Feb 27, 2013 at 9:33
  • It does'nt show any rows so i will try your suggestion Commented Feb 27, 2013 at 9:34
  • i forgot to tell i have another error. i posted it above Commented Feb 27, 2013 at 9:37
  • Sounds like your query doesn't return any results, and so the array passed to the view holds no data, and can't be looped through. Commented Feb 27, 2013 at 9:38
  • Don't know why this happens because there is data in my table and i Thought the code was good. Commented Feb 27, 2013 at 9:39

3 Answers 3

1

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);
    }

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

2 Comments

allright. i am very stupid. please hit me with a stick. thanks so much :D accepted
No problem at all, by the way instead of loading all that views, in every method you could adopt a "templating" system, in this answer I gave a very basic way of do it stackoverflow.com/questions/15093949/… And you also have this way stackoverflow.com/questions/8601054/…
1

Use this in your controller:

<?php 
if(is_array($query) && count($query)  > 0 ) { ?>
<ul>
<?php foreach ($query as $row): ?>
    <?php echo $row->Categorie; ?>
<?php endforeach; ?>
</ul>
<?php }?>

4 Comments

&& count($query) > 0 is odd here. If there're no elements loop body will never execute.
You mean views? and this doesn't work either. still the same error.
Hmm something changed now. the second error is gone. but the first error is still showing up
Can you help me any further with this?
-1

Change your controller to this

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

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

And change view to

<ul>
<?php foreach ($list as $row){?>
    <?php echo $row->Categorie; ?>
<?php } ?>
</ul>

1 Comment

Doesn't work. get the same error only list is different Severity: Notice Message: Undefined variable: list

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.