Get recursive menu from database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Get recursive menu from database (/showthread.php?tid=17986) Pages:
1
2
|
Get recursive menu from database - El Forum - 04-21-2009 [eluser]mvdg27[/eluser] Hi guys, I've been breaking my head all night, on getting a recursive menu from my database! I hope someone can give me some pointers on how I would be able to achieve this. My database structure is very simple: id|label|parent And I'm trying to achieve an array with the following structure as a result of my menu-retrieval function: Code: Array Hope someone can help me on my way with this one! Thanks! Get recursive menu from database - El Forum - 04-21-2009 [eluser]Dam1an[/eluser] Although the code below isn't what you asked for, its some old code I had which done a nested drop down list of categories, so same concept. Its essentially a recursive call starting with the top level categories, then searching for categories which have that as its parent etc If you can't make sense of it, I'll adopt it to your situation in the morning, but I'm going bed now Code: // Creates a list of all the categories, nested within each other, used when On a side note, my old (pre CI) code was hideous Get recursive menu from database - El Forum - 04-21-2009 [eluser]Clooner[/eluser] Check out this topic http://ellislab.com/forums/viewthread/108528/ Get recursive menu from database - El Forum - 04-22-2009 [eluser]mvdg27[/eluser] @Dam1an: thanks for your example, this was something I sort of had already .. printing directly to the screen and using a loop over the depth with ' ' to indicate the level in the menu. My problem however is how to get everything in an array .. I'll play around some more with your example, but in case you're able to whip up an example easily, by all means Thanks! @Clooner: Thanks for your suggestion, I've looked into MPTT before, but since my menus will never be very deep, I prefer keeping the simpler 'parent->child' model Get recursive menu from database - El Forum - 04-22-2009 [eluser]Dam1an[/eluser] Ok, here's some quick code I've mocked up Code: <?php and with the array in your example, it would generate the following array Code: Array ( You'll obviously want to move the function into a helper, but you get the idea (hopefully) If not, I'll explain it in detail Get recursive menu from database - El Forum - 04-26-2009 [eluser]mvdg27[/eluser] Hi Dam1an, I wasn't able to respond earlier. But your code has helped me to make the exact recursive function I need. Thanks a lot, this method works like a charm! Cheers, Michiel Get recursive menu from database - El Forum - 04-26-2009 [eluser]JoostV[/eluser] Damian's solution does a seperate query on every parent to obtain its children. You could end up doing twenty or more queries just to fetch a single tree. You might want to Google for 'adjacency list sql' or check out MPTtree: http://ellislab.com/forums/viewthread/74114/ Get recursive menu from database - El Forum - 04-26-2009 [eluser]Dam1an[/eluser] [quote author="JoostV" date="1240789405"]Damian's solution does a seperate query on every parent to obtain its children. You could end up doing twenty or more queries just to fetch a single tree. You might want to Google for 'adjacency list sql' or check out MPTtree: http://ellislab.com/forums/viewthread/74114/[/quote] I never claimed my solution was perfect, but it gets the job done, and if/when performance ecomes an issue, its all a self contained function, so shoud be easy ehough to update... Although I should really look into adjacency list sql lol Get recursive menu from database - El Forum - 04-26-2009 [eluser]JoostV[/eluser] @Dam1an: no offence meant, Dam1an. Get recursive menu from database - El Forum - 04-26-2009 [eluser]Dam1an[/eluser] @JoostV: No offence taken, I just wanted to clear up the fact I know my solution wasn't ideal Looking at this adjacency list sql... looks fun Has nobody made a function you could add to the DB class for this sort of thing, cause I've seen stuff like this come up a few times on the forums (if not, I might have a go at hacking a generic function together) |