tags:

views:

150

answers:

3

I have some data that I have to display as a table.

I think I should pass the data from the controller as $data['events'] = array(.....

and then load the view to display them.

        <?php
    $this->load->library('table');

    echo $this->table->generate($events);
    ?>

this doesn't work though - it gives a Fatal error: Call to a member function generate() on a non-object

If I paste the same code in the controller, obviously using ->generate($data['events'] the table gets displayed correctly.

Should I get that views can't load libraries, or I am doing something wrong? Or maybe should I capture the output of the library in the controller and send that to the view?

+1  A: 

You should run below code in the controller:

<?php
$this->load->library('table');

echo $this->table->generate($events);
?>

and store the data in variable and then send to the view.

Sarfraz
A: 

Hi,

That is one wrong approach to MVC. You don't need to load the library in the view, because all views are loaded from one CONTROLLER, so every external Helper or Library should be loaded from the controller and the used or send to the views

Regards,
Pedro

Pedro
A: 

Note the shoulds- CodeIgniter is flexible in that it lets you do things the wrong was- just makes it a little more difficult. You can do almost everything in the view, even when you shouldn't, but loading helpers, models, and views has to be done in the controller.

Stiggz