![]() |
Menu from mysql in view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Menu from mysql in view (/showthread.php?tid=48744) |
Menu from mysql in view - El Forum - 01-27-2012 [eluser]Unknown[/eluser] Hi there, if I want to load menu items from mysql it would be very comfortable to do it in my view file: Code: <ul> But, according to MVC, it is bad practice, because all actions with data must be in model... What would be the best way to do mysql menu? Sorry for my bad english ![]() Menu from mysql in view - El Forum - 01-27-2012 [eluser]Krystian[/eluser] try to 1. create model method that retrieve entries form menu table and returns an array 2. in Controller use this method and pass its results to the view 3. in view just use foreach loop to echo results ![]() Menu from mysql in view - El Forum - 01-27-2012 [eluser]Jason Stanley[/eluser] If this menu is in its own little snippet then I don't think you are doing a great deal wrong. Its a pretty simple menu. If you convert the database object to an array then you would still have essentially the same.. Code: <ul> The main thing to change is to stop calling the model from the view. Instead pass the view file a variable containing the menu object. I would only do more in the model if the navigation was more complex. If there were submenus etc which required a bit more logic I would create an array then probably a helper function to convert the array into html. Menu from mysql in view - El Forum - 01-27-2012 [eluser]LuckyFella73[/eluser] I would place the db query in a model for its a cleaner solution. If you need the menu data in several controllers you will have to repeat the code again and again. If you place the db query in a model you can also set up a method formatting the db result with needed html/ css and return the whole html menu part (and send that to your view). In case you have to change the html of your menu you have everything in one place. Having the query in your view files would force you to edit up to maaany lines in case something changes. |