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

[eluser]egguzel[/eluser]
Hi,

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?

Thanks
#2

[eluser]PhilTem[/eluser]
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

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

[eluser]solid9[/eluser]
@egguzel

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.
#4

[eluser]egguzel[/eluser]
Hi,

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.
#5

[eluser]solid9[/eluser]
Better post your model, view and controller codes.
So people here can help you.
#6

[eluser]egguzel[/eluser]
Hi everyone,

I found my answer.

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

$CI =& get_instance();
$CI->load->library('library_name');
$CI->library_name->My_Function();

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

[eluser]PhilTem[/eluser]
That's more or less what I was pointing out above Wink
#8

[eluser]solid9[/eluser]
at the end of that road there is a monster.
funny. ^__^




Theme © iAndrew 2016 - Forum software by © MyBB