11

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?

8 Answers 8

28

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();
Sign up to request clarification or add additional context in comments.

2 Comments

If your library is in a static context, then, simply do the following to avoid get-instance() overhead: <?php require("application/libraries/<library_name.php>"); Library_name::yourFunction(); ?>
@Christina I actually wanted to load the library in external class and after searching for hours couldn't find the solution, Until I came across your answer. That was really helpful. Also, if you know any other method for same then please let us know.
14

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.

1 Comment

My library takes data from a model and then prepares markup for inclusion in the view. Since I'm dealing with markup seems like that should happen in the view and not the controller. I'm being academic but perhaps you see my point.
5

To answer what you are doing wrong, you should know that the CodeIgniter class isn't declared in the view, and that this is the case for a reason - to abstract your PHP code from your HTML. Views should contain minimal PHP code (basic loops, conditions).

With this in mind, you should include your library as normal in the controller as follows;

controller

$this->load->library('table');
$data['events_table'] = $this->table->generate($events);

$this->load->view(....);

In the view, you simple echo the data. Although CodeIgniter allows short-hand tags, you should use standard PHP tags to keep to a convention that will work anywhere you keep your code.

view

<?php echo $events_table; ?>

Comments

5

You can auto-load the library in config/autoload.php

$autoload['libraries'] = array('database','session','table');

Then you can simply call the functions in your view .

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

This was useful for me when i was creating a dynamic menu header . i auto-loaded the library which has the function for dynamic menu creation and then i simply called that function from the view , by the way it is bad practice .

Comments

3

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

Comments

2

Note the shoulds: CodeIgniter is flexible in that it lets you do things the wrong way. It 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.

1 Comment

I agree with karimkorun. If you are the sole programmer on a project, what you do doesn't matter. Part of the appeal of MVC is helping multiple programmers sharing a code base to keep the logic of the code organized, so EVERYONE knows where to look. As others have said, data should be assembled and organized in the controller with the only logic in the view related to output and formatting. Using Helper functions further organizes any common output and formatting logic and their use is welcomed in the view files.
1

In controller or model use

$this->load->library('mylib');
$mylib=$this->mylib;
$data['mylib']=$mylib;
$this->load->view('myview',$data,false);

and than in view you can use library directly:

$mylib->myfunction();

It's not MVC like solution but works if you need.

Comments

1

Simply load library on controller, then use it view.

Controller:

$this->load->library('table');
$data = array(
    array(1,2,3,4,5),
    array(1,2,3,4,5),
    array(1,2,3,4,5),
);
$this->load->view('the_view', compact('data'));

View:

echo $this->table->generate($data);

Comments

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.