Reputation: 10156
Looking at frameworks I came across Codeigniter. I was wondering what the proper process would be to build the html output for results from the db before passing it to the view. Meaning I create a model to fetch rows from the db, load the model in the controller, before passing the results to the view I want to build the view using the results instead of looping through the results and displaying it in the view itself. If I were to do that would I create a private function in the controller to do that? Or would I do that in the model or should I just do that in the view?
I just feel this would be cleaner to do than write all the code in the view to display the results so if there were changes to the display of the results or display it on another page I can just re-use the display function instead of copying and pasting the code from the view.
Upvotes: 1
Views: 322
Reputation: 102854
I just feel this would be cleaner to do than write all the code in the view to display the results so if there were changes to the display of the results or display it on another page I can just re-use the display function instead of copying and pasting the code from the view.
No need to do that at all, simply write your display logic into a view file as mentioned, and load the same view file for each instance you want to display the results the same way.
This is exactly what the view layer is for: Presentation. Remember that you don't need a separate view for every page. Likewise, you can make several views for the same data, so you can switch between them (or even let your users switch between them).
Upvotes: 1
Reputation: 24989
The view is the information that is being presented to a user. That's where you should be looping through your results.
Have a look at http://codeigniter.com/user_guide/overview/mvc.html for more an overview of what the models, controllers and views are responsible for.
Upvotes: 2
Reputation: 62412
I would recommend putting view specific code in, well ... the views.
You might think it would make it cleaner because it makes your view look tidy, but honestly once you have the view output coming from all directions, it could get very complex, very fast.
And in my experience, I hardly ever have a single, ever consistant, representation for a returned database object.
Upvotes: 3