Welcome Guest, Not a member yet? Register   Sign In
How to call library or helper from view


In my view files, I use foreach loops and there is a huge function (this function is identical for all views) in these different foreach loops.

I want to write down this function into a library and I want to call this library and function from my different views.

I searched about this but I could not find anything.

Could you help me.

How can I call library and this function from my view files?


You need to load the library/helper in your controller (it's even possible but not best practice to do it in the view) and then you can just use it like

function_name($arguments, ...) // for the helper function
$this->library->method($arguments, ...) // for the library function


PhilTem is correct.
You should follow the MVC rule.
Process your data in the controller or model.
The view is where you display the already processed data.
Better read the User Guide.


I already know "MVC" and "it is not good to call library from view". But there is a point that I can not understand.

I use foreach loop in a view. When I load this view, I execute a function may be 200 times or more depends on the database results.

Is it possible to use still MVC to call a library 200 times and get 200 different results when I load a view once.

If this is not possible (I could not find a way to do that), I should call this library and function directly from view, right? Can I do that?

Thanks for your answers.

Better post your model, view and controller codes.
So people here can help you.

Hi everyone,

I found my answer.

I can call the library from view with the code below.

$CI =& get_instance();

I hope this code will also help everyone who needs it.

That's more or less what I was pointing out above Wink

at the end of that road there is a monster.
funny. ^__^

Theme © iAndrew 2016 - Forum software by © MyBB