Implementation of hierarchy of categories
I have task to implement hierarchy of Category->Sub-Category->Offer->Offer Details. But I'm not sure how to do this. I should use JSON to fill date. My idea is the root controller "CategoriesController" to make HTTP request to the ser开发者_运维百科ver and parse the response and names of the cells to be rows of my table view. But I don't know how to second controller "SubCategoriesController" to know who is him parent. I mean how to make the request to return me only categories that are children of the selected category. Please give me some guidelines.
Thanks :)
I have done this like follows:
Table structure should be like:
Category_id, parent_category_id, category_name, etc
All main categories should have parent_category_id = 0
So when you want to load all the main categories you will load where parent_category_id = 0
Similarly all categories will have category_id in their parent_category_id.
Example:
Apparels->Ready Made->Pent->Skinny
Apparels is parent of Ready Made Ready made is parent of Pent Pent is parent of Skinny
So in DB is should be like this
Name : category_id : parent_category_id
Apparels : 1 : 0 (This does not have any parent)
Ready made : 2 : 1
Pent : 3 : 2
Skinny : 4 : 3
So if you want to know the parent of Skinny you will check the value in parent_category_id that is 3 for this example then you will check the category_id = 3 and that is Pent which is correct for this example.
I hope this will help you :)
Thanks.
精彩评论