EllisLab text mark
Advanced Search
     
How to call library or helper from view
Posted: 11 November 2012 04:38 PM
Joined: 2012-06-28
12 posts

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

 
Posted: 11 November 2012 05:59 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

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 
 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 11 November 2012 07:46 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
536 posts

@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.

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 12 November 2012 02:19 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-06-28
12 posts

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.

 
Posted: 12 November 2012 02:24 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
536 posts

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

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 12 November 2012 02:03 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-06-28
12 posts

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.

 
Posted: 12 November 2012 02:37 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

That’s more or less what I was pointing out above wink

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 12 November 2012 08:06 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
536 posts

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

 Signature 

Stick with it, practice it and have fun with it.