![]() |
How to call model function in view file? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: How to call model function in view file? (/showthread.php?tid=32578) |
How to call model function in view file? - El Forum - 07-27-2010 [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) { Please advise me on this. Thanks a lot. -Mak How to call model function in view file? - El Forum - 07-27-2010 [eluser]danmontgomery[/eluser] Code: $CI =& get_instance(); 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" How to call model function in view file? - El Forum - 07-28-2010 [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. How to call model function in view file? - El Forum - 07-28-2010 [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. How to call model function in view file? - El Forum - 07-28-2010 [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 |