Send data from Controller to View or have logic within View?
I have a webpage where I am trying to create a contextual menu depending on the category of the page being browsed. If the category is on animals
, the segment based url will contain animal
, ie. http://www.mywebbie.com/livingthings/display/category/animals
.
To build the contextual menu, I will need to access my database table subcategory
with columns subcat_id
, subcat_name
, cat_id
and select all rows w开发者_运维知识库ith cat_id
corresponding to that of animals
.
Should I access the database (via the model method) from the controller, then pass the array containing the subcategory names to the view? Or should everything be done within the view, where there is code that loads the model method that returns an array of subcatergories?
Btw, I'm using PHP framework Codeigniter.
Yes, The controller takes care of communicating with the model and handling the data, the view takes care of presenting the data.
Make the data available to the view from the controller, in the most simple form, lets say a simple array.
Not familliar with CI, but if you have some kind of ActiveRecord or ORM, then add a getAllSubCats() method to the category(?) entity and call it when you loop through categories.
If the above doesn't make sense: View shouldn't even know a database exists, so to answer your question, do it in the Controller. Or better yet, do the fetching in the Model and pass the results back into the Controller.
精彩评论