Welcome Guest, Not a member yet? Register   Sign In
How to call model function in view file?
#1

[eluser]makkal[/eluser]
Hi all,

I am new to CI. I like to know the way to access the model function in view file.

While processing the FOR loop in view file, i should call database to fetch some more values for the particular value executing in the LOOP.

Code:
foreach($navlist as $parent => $menuid) {
  
   [with this $menuid, i need to call its relevant values from the database]  

}

Please advise me on this. Thanks a lot.

-Mak
#2

[eluser]danmontgomery[/eluser]
Code:
$CI =& get_instance();
$CI->model_name->method_name();

This is covered in the user guide under "Utilizing CodeIgniter Resources within Your Library", but a more appropriate title would be "Utilizing CodeIgniter Resources from anywhere other than the controller"
#3

[eluser]mddd[/eluser]
noctrum is right.

I do want to point out that it is bad practice to go back and get stuff from the database when you are already in the view.
If you need more information about (for instance) the menu structure, you should load it before, and pass it to the view so the view can just display it.
It's best to keep the view as simple as possible and not do 'smart' stuff like database calls in there!!
Also, if you do it like you suggest, you'll make a database call for every item in the navlist! It is much more efficient to get all the relevant info in one database request and then pass that info to the view.
#4

[eluser]Zeeshan Rasool[/eluser]
Agreed with mddd, you can do model call in view but its against MVC structure. Get all data in controller and send just output to view.
#5

[eluser]makkal[/eluser]
Thank you Guys,

Let me do it in the MVC structure. Could you suggest me the best ebook and learn more about the CI. Now i am good in the basics in CI, i want to know more about building the applications.

Kindly suggest me a book. Thanks again.

-Mak




Theme © iAndrew 2016 - Forum software by © MyBB