Categories, subcategories, items |
[eluser]roadie[/eluser]
Hi guys! I need some help and I'd really appreciate it. I'd like to create an application, something like a business directory, but not very complicated. The structure is like this: Category1 Category1-1 Item1 Item2 Item3 Category1-2 Item4 ......... Categroy1-3 Category2 and so on..... I need some help with the model. I've been trying for few days and I can't figure it out how to do it. I'm new in CI and your help would be really appreciate it.
[eluser]ciGR[/eluser]
Hi, first of all you must set your database structure. For example you can set the table for categories like Code: cat_id | cat_name | parent_cat_id for example the Category1-1 has parent_id the cat_id of the category1, also the Category1-2 ... The top categories like Category1, Category2 you can set parent_id = 0, to separate from the others. another approach id to create 2 tables in your database first Code: cat_id | cat_name and a second with Code: id | parent_cat_id | sub_cat_id | for example in the second table you may have Code: 1 | Category1-id | Category1-1-id and work with joins queries.
[eluser]roadie[/eluser]
Hi! Thanks for your reply. I'm thinking to create 2 tables anyways, something like this. 1.Category id parent_id name description 2.Item id parent_id name content The Item will have the actual content. I wrote some simple controller, model and view but I could make it retrieve only the top-level categories. <<Controller>> ........ function category() { $this->load->model('category_model'); $data['cats'] = $this->category_model->getCategory(); $this->load->view('categories_view', $data); } <<Model>> ...... function getCategory() { $query = $this->db->get_where('category', array('parent_id' => 0)); return $query->result(); } function getSubcats($id) { $query = $this->db->get_where('category', array('parent_id' =>$id)); return $query->result(); } <<View>> ........ <?php foreach($cats as $cat): ?> <div> <h1><?php echo $cat->name; ?></h1> <?php $subcats = $this->category_model->getSubcats($cat->id);?> <ul> <?php foreach($subcats as $subcat): ?> <li><?php echo anchor('site/category/'.$subcat->id, $subcat->name); ?></li> <?php endforeach ?> </ul> </div> <?php endforeach ?>
[eluser]slowgary[/eluser]
Wrap your code in [ code ] tags next time, it makes it easier for people to help. Calling your model from the view is kind of a no-no. While codeigniter is not overly restrictive and will LET you do so, you should not do so for good, clean, maintainable MVC structure. Before you can get good help, there are a few questions to ask. Do you ever plan to have more than 2 levels of categories? If you'll only have top-level and second level categories, it will be easier to construct your navigational tree. New question... Will your categories be alphabetized? Also, will you show visitors the entire navigational tree, or will they only see the top level category and need to click on a top level before seeing its subcategories? I wouldn't create a "get_sub_categories()" method since it implies that you may be doing LOTS of queries per page load just to show visitors the navigation. You're better off getting all the categories in a single query and sorting them out in your controller before passing them to the view. Assuming alphabetized categories, and also assuming that your tree will be max of 2 levels and entirely visible to the visitor, you could do something like this (pseudocode): Code: //model If you need more than 2 levels of navigation, you'd want to throw some of that code into a function and call it recursively. I hope this helps.
[eluser]roadie[/eluser]
Thanks slowgary! I will have only 2 levels of categories which I want to show them from the begining. Category1 Category1-1 Category1-2 Category1-3 Category2 Category2-1 Category2-2 .............. Then I need another view to show the subcat with it's items. Can I use the same view but just add a condition or I should do another view? Code: if cat_has_no_child(){ //the category is a subcat-> all the children are items Category1-1 Item1 Item2 Item3 ...... And then the last view the Item itself.
[eluser]slowgary[/eluser]
Either works... I'm not sure of your exact application, but I'd probably have a view that contains the navigation, a view that shows a list of items, and a view that shows a single item and all of it's details. As far as the actual frontend goes, I'd likely include the navigation view on all the pages... so a user would either see a bunch of categories on the left and a list of items on the right, or they'd see a bunch of categories on the left and one item (detail view) on the right. Throw something into your categories view to add a CSS class to a category if it's currently being viewed and you're good to go. Good luck.
[eluser]xeroblast[/eluser]
as what i learn in hierarchical data, there are 2 types of model... the adjacent model and the nested model... and it is easier if your so much of the 2... for more info: check this out: http://dev.mysql.com/tech-resources/arti...-data.html
[eluser]Zeeshan Rasool[/eluser]
You can put child id . parent id so that we can get both ids (child,parent from a single field). Just save in db 1.2 1.3 1.4 etc
[eluser]slowgary[/eluser]
Roadie, There are different ways to store your categories in the database, as xeroblast mentioned. The method you're using is an adjacency list, but I wouldn't get caught up on that. Stick with what you have, it's simplest and will work well for what you're doing. As far as combining 2 fields into 1 using a period, I wouldn't recommend it. Keeping your data separate and normalizes will be better in the long run. You're on the right track. |
Welcome Guest, Not a member yet? Register Sign In |