vincej Posted May 10, 2013 Share Posted May 10, 2013 Hi - I am struggling to find a solution to a category problem. Perhaps I am over thinking this. I need to display a navigation menu in tabs so that as you tab through the hierarchy you can see the categories in the hierarchy both above and below where you currently are. Conceptually I found an architectural solution on Sitepoint called, Modified Preorder Tree Traversal - but it is quite complex and I was hoping for something a little easier. In simple terms I need to be able to search and display all the categories and sub categories going up or down the category hierarchy. It could be as much as 5 levels deep. My need is 3 fold: 1 - starting at the root I have to find all the descendants. 2 - if you start at the bottom of the hierarchy, I need to find all the ancestors. 3 - Worst of all ... if you start half way down the hierarchy, I need to display both all the ancestors and all the descendants. Does anyone have some advice or resources I should consider ?? MANY MANY Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/277897-need-help-with-searching-categories/ Share on other sites More sharing options...
requinix Posted May 10, 2013 Share Posted May 10, 2013 Couple things to take into account: - How often will you add new categories? Move or remove ones? - Do you only need all ancestors and all descendants? Will you ever need to know only immediate parents or immediate children? Quote Link to comment https://forums.phpfreaks.com/topic/277897-need-help-with-searching-categories/#findComment-1429597 Share on other sites More sharing options...
Barand Posted May 10, 2013 Share Posted May 10, 2013 You need two arrays One - item as key and parent as value Two - parent as key and array of children as value Then you need two recursive functions to traverse each array Job done. Quote Link to comment https://forums.phpfreaks.com/topic/277897-need-help-with-searching-categories/#findComment-1429600 Share on other sites More sharing options...
vincej Posted May 13, 2013 Author Share Posted May 13, 2013 @requinix I will rarely need to add or remove categories. And I will never only want immediate ancestors or descendants One thing that I omitted to mention is that, the number of levels will be of a fixed and known in advance. I need to go down 5 levels. so for example: Fruit -> Red -> Apples -> Californian -> Red Delicious | Galla | Jersey Mac | -- 5 levels So, if I click "Fruit" I might see "Fruit | Red | Green | Yellow" all in tabs -- current Root level + the next level If I click "Red" I might see "Fruit | Red | Californian | Mexican | Florida all in tabs -- 1 ancestor + next level of descendents If I click "California" I might see "Fruit | Red | Apples | Californian | Red Delicious | Galla | Jersey Mac " -- 3 ancestors + next level of descendants So the point is the customer can see where he has come from ( ancestors) and where he can go ( descendent ) and with 1 click jump straight back to "Red" if they choose. Also they can see the descendants beyond where they are as well. So I am thinking a 5 way multi-dimensional array. I can populate the array in hard code. Where I am coming unstuck is is how best to dynamically pull the correct content out of the array based on what the user clicks. Many Many Thanks for your Help ! Quote Link to comment https://forums.phpfreaks.com/topic/277897-need-help-with-searching-categories/#findComment-1429926 Share on other sites More sharing options...
requinix Posted May 13, 2013 Share Posted May 13, 2013 (edited) You're contradicting yourself... And I will never only want immediate ancestors or descendants So, if I click "Fruit" I might see "Fruit | Red | Green | Yellow" all in tabs -- current Root level + the next level...In those cases you do only want immediate descendants. Unless you're talking only about what is visible to the user at a given time - are you constructing the entire menu hierarchy at once and showing/hiding trees as the user accesses the menu? Edited May 13, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/277897-need-help-with-searching-categories/#findComment-1429927 Share on other sites More sharing options...
vincej Posted May 13, 2013 Author Share Posted May 13, 2013 Fair comment, yes, well pointed out - I will only want immediately descendants however, I do want to see all ancestors, such that t he click can jump multiple steps up the tree in one hit. are you constructing the entire menu hierarchy at once and showing/hiding trees as the user accesses the menu? I'm not sure the best way to hold the data ie one node at a time or all in a single big array. I have it working in a cms ( ExpressionEngine) using an extension. But I want to move off ExpressionEngine and recreate it in PHP somewhere else. You can see it in a sandbox environment : http://jacobssoftwareanddesign.com/angelsroofing/index.php/sub_category/roofing Many Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/277897-need-help-with-searching-categories/#findComment-1429932 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.