formsim Posted October 16, 2009 Share Posted October 16, 2009 Hey guys, Been programming for a while but finally ran into a problem i can't figure out. I got a mysql table with data like follows. ID - PARENT ID - ITEM NAME 1 - 0 - Physical Container 1 2 - 0 - Physical Container 2 3 - 2 - Box Inside Container 2 4 - 2 - Another Box Inside Container 2 5 - 3 - 1 Item Inside Box Inside Container 2 6 - 3 - 2 Item Inside Box Inside Container 2 7 - 2 - Item Inside Physical Container 2 Don't laugh, I didn't create the table, this is how the data is being gathered and stored sadly. What i'm trying to do is create a menu tree style from an array as follows. - Physical Container 1 + Physical Container 2 + Box Inside Container 2 - 1 Item Inside Box Inside Container 2 - 2 Item Inside Box Inside Container 2 - Another Box Inside Container 2 - Item Inside Physical Container 2 I've tried multiple nested loops to create the parent interface arrays and try to reassemble into 1 multi array to export to my front end code but all my logic has been flawed. Closest i have gotten is the menu system only 1 parent interface deep. Then my code is completely lost with what to do next. Any suggestions by the experts? Link to comment https://forums.phpfreaks.com/topic/177970-parent-sub-parent-multi-array/ Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 The downside to an adjaceny list model is that you must write a recursive function (involving multiple queries to the database) if you do not already know the depth beforehand. Check out this article on SitePoint. If it does not help, let me know and I'll help you out. Link to comment https://forums.phpfreaks.com/topic/177970-parent-sub-parent-multi-array/#findComment-938363 Share on other sites More sharing options...
formsim Posted October 16, 2009 Author Share Posted October 16, 2009 And didn't even know it had a name sadly the mutli query solution is what i've been leaning towards as my last option, but the list is sometimes 100-150 items deep and i just wouldn't sit right know its hitting the db that many times in the background. Let me take a look at your link since it seems to nail my problem on the head. Big thankx Link to comment https://forums.phpfreaks.com/topic/177970-parent-sub-parent-multi-array/#findComment-938367 Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 If you want the easy way out without using a recursive function and multiple queries.. http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/ There are disadvantages to this approach though, such as it is much more difficult to find a specific node. Link to comment https://forums.phpfreaks.com/topic/177970-parent-sub-parent-multi-array/#findComment-938369 Share on other sites More sharing options...
formsim Posted October 16, 2009 Author Share Posted October 16, 2009 This nailed my problem head on. Being able to add children to a parent deep inside a tree. This looks like it would work but it sounds like your somewhat against the practice of using references to solve this purpose. Is there a downside that you are aware of? Link to comment https://forums.phpfreaks.com/topic/177970-parent-sub-parent-multi-array/#findComment-938371 Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 No I'm not against it at all. Most times, I would probably actually use the single-pass method (using references). If you need to only display the tree down to a certain node (child) however, it may be easier to use the recursive function method. Link to comment https://forums.phpfreaks.com/topic/177970-parent-sub-parent-multi-array/#findComment-938374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.