Hey guys,
I'm trying to stick to the MVC standards while developing my code. I have come across a small problem, I need a foreach loop to echo out a table cell for every entery in a database table. In MVC you are supose to keep basic logic in the controller and out of the view and keep view code out of the controller, asuming that this view has some code that should not be repeated based on the foreach how can i still loop the parts that need looped withought creating a whole lot of views? It dosent seem posible, if its not what is the correct way to solve this problem while sticking to the MVC guidelines?
MVC foreach loop?
Started by zeroradius, Mar 31 2010 03:05 PM
2 replies to this topic
#1
Posted 31 March 2010 - 03:05 PM
|
|
|
#2
Posted 01 April 2010 - 07:13 AM
For instances like that, it isn't logical to have it in the controller. I use MVC and I have a few loops in the templates. You should not build HTML in your controller only the view should handle HTML so it would be acceptable to use it in the view file. If you are following some strict guidelines and if you really want, you could generate the HTML in the controller and assign it to a variable for output. I'm sure some will argue, but MVC is a design pattern theory, it isn't concrete and it can be improved. Also, guidelines are not rules....
This is my opinion on the matter. I have been using MVC for a few years and I use minimal loops and conditionals in my views, only in areas that are needed. Your problem sounds like a valid reason to use a loop in the view.
Hope you find your answer. :)
This is my opinion on the matter. I have been using MVC for a few years and I use minimal loops and conditionals in my views, only in areas that are needed. Your problem sounds like a valid reason to use a loop in the view.
Hope you find your answer. :)
#3
Posted 02 April 2010 - 06:33 PM
Ok, thanks. I am trying to make a site using MVC for my portfolio and i wanted to make sure i was doing everything right. My first thought was to put the loop in the view but I had that voice in the back of my head going "Your probably wrong, ask someone who knows how it is supose to go" Thanks agin!


Sign In
Create Account


Back to top









