Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter - loading a library from a view?

Tags:

codeigniter

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?

like image 421
Patrick Avatar asked Apr 12 '10 14:04

Patrick


People also ask

Which function is used to load a library in CodeIgniter?

You will load it using: $this->load->library('flavors/chocolate'); You may nest the file in as many subdirectories as you want. Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.

Can we load library in model CodeIgniter?

Yes, but loading libraries where they're needed minimizes dependency issues.

Where do I put library in CodeIgniter?

All of the available libraries are located in your system/libraries/ directory. In most cases, to use one of these classes involves initializing it within a controller using the following initialization method: $this->load->library('class_name');

Can the CodeIgniter view be load from the model?

You don't access the model from the view. You access it from the controller and provides the output to the view.


2 Answers

If you need to call a library (and its functions) within a view, you can do this:

$CI =& get_instance();
$CI->load->library('library_name');
$CI->library_name->yourFunction();
like image 114
Christina Huggins Ramey Avatar answered Oct 07 '22 06:10

Christina Huggins Ramey


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.

like image 36
Sarfraz Avatar answered Oct 07 '22 07:10

Sarfraz