ram4nd Posted January 13, 2010 Share Posted January 13, 2010 I want to make dropdown menu. I have a table where are categories and x amount of subcategories. They will be displayed something like: cat -subcat -subcat --subcat cat -subcat I figured I need some loop system. I was trying to do it with 2 foreach loops in while loop, but I got confused and I thought I'll ask help from people with more brain processing capabilities. Quote Link to comment https://forums.phpfreaks.com/topic/188366-categories-dropdown-with-x-amount-of-subcategories/ Share on other sites More sharing options...
simshaun Posted January 13, 2010 Share Posted January 13, 2010 How are you storing the hierarchy? Adjacency list (parent id) or a Modified preorder algorithm (left and right values)? Quote Link to comment https://forums.phpfreaks.com/topic/188366-categories-dropdown-with-x-amount-of-subcategories/#findComment-994403 Share on other sites More sharing options...
ram4nd Posted January 13, 2010 Author Share Posted January 13, 2010 Adjacency list (parent id) or a Modified preorder algorithm (left and right values)? I dont understand a word of it, but maybe this helps: CREATE TABLE IF NOT EXISTS `categories` ( `id` mediumint(9) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `category_id` mediumint(9) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=26 ; Quote Link to comment https://forums.phpfreaks.com/topic/188366-categories-dropdown-with-x-amount-of-subcategories/#findComment-994415 Share on other sites More sharing options...
simshaun Posted January 13, 2010 Share Posted January 13, 2010 I assume category_id is the parent_id? If so, you are using an adjacency list, and you should check out this blog article. Quote Link to comment https://forums.phpfreaks.com/topic/188366-categories-dropdown-with-x-amount-of-subcategories/#findComment-994451 Share on other sites More sharing options...
ram4nd Posted January 13, 2010 Author Share Posted January 13, 2010 I assume category_id is the parent_id? If so, you are using an adjacency list, and you should check out this blog article. Yes you assumed right. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/188366-categories-dropdown-with-x-amount-of-subcategories/#findComment-994455 Share on other sites More sharing options...
ram4nd Posted January 14, 2010 Author Share Posted January 14, 2010 The category_id is now parent_id I did it like this: function get_categories() { $query = $this->db->query('SELECT * FROM categories ORDER BY name'); //return data if($query->num_rows()>0) { static $refs = array(); static $list = array(); foreach($query->result_array() as $data) { static $id, $parent_id, $name; $id = $data['id']; $parent_id = $data['parent_id']; $name = $data['name']; $thisref = &$refs[$id]; $thisref['parent_id'] = $parent_id; $thisref['name'] = $name; if($parent_id == 0) $list[$id] = &$thisref; else $refs[$parent_id]['children'][$id] = &$thisref; } } else return FALSE; } But I can't figure out how can I echo it? And yes I read the comments. Quote Link to comment https://forums.phpfreaks.com/topic/188366-categories-dropdown-with-x-amount-of-subcategories/#findComment-994774 Share on other sites More sharing options...
PravinS Posted January 14, 2010 Share Posted January 14, 2010 May this will help you, its a simple dynamic multilevel menu https://sourceforge.net/projects/multilevelmenu/files/latest/ Quote Link to comment https://forums.phpfreaks.com/topic/188366-categories-dropdown-with-x-amount-of-subcategories/#findComment-994779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.