More recursive madness |
[eluser]skunkbad[/eluser]
A couple of days ago I was working on a recursive function that builds the category menu for my ecommerce application. I finished that with code handed to me by Tominator, and then I went on to work on the way that the new URLs were being handled by the product controller / model, so that the appropriate category would show products or sub-categories, even if more than one category had the same name. So last night I finished up on yet another recursive function. This time I got it right, but I'm wondering if it couldn't be simplified. Since I'm not so good with recursive functions, perhaps you could offer advice. I'm just going to post a link to the file on Bitbucket: http://bitbucket.org/skunkbad/community-..._model.php I'm specifically looking at lines 80 through 157, which includes two methods: 1) get_category_steps() 2) get_category_parents() ALSO, The make_category_menu() method in the same file is not recursive. It only goes 7 levels deep, which I thought would be enough for most stores, but if somebody has a truly recursive way to do what I need done here, I would love to see the solution. Any help is greatly appreciated, and you can receive credit in the code for your help if you wish.
[eluser]Federico BaƱa[/eluser]
Try something like this for the cateogry menu: Code: // Lets see.. hope this helps.. about the other recursives i cant figure out what you want to get there, what is $segs actually? (in the first method)
[eluser]Mischievous[/eluser]
I would need to see your db setup but I have a couple functions that do this. That allow an unlimited amount of levels using 1 db request.
[eluser]skunkbad[/eluser]
[quote author="Mischievous" date="1271830145"]I would need to see your db setup but I have a couple functions that do this. That allow an unlimited amount of levels using 1 db request.[/quote] The DB is like this: http://bitbucket.org/skunkbad/community-...y_cart.sql Currently, the constructor brings in all category table data and creates a result array. That array is used by a a lot of the methods, so I need to use that array to build the menu. There is a recursive function in the category_menu view that takes what the make_category_menu() method passes to it and creates the menu. Maybe there is a more direct way, or a more simple way. Like I said earlier, it does work, but if there was some optimization or simplification, everyone that uses the application would benefit.
[eluser]skunkbad[/eluser]
Probably the best way to approach what I am trying to do is simply show the result array, and then the processed/sorted array. This is the result array before sorting: Code: Array This is the array after sorting: Code: Array This array goes into the view where the category menu is built with yet another recursive function. I am probably just overworking this....
[eluser]Mischievous[/eluser]
Builders: Code: var $locations = array(); Display[ers] Code: function formatMenu($menu) Code: $menu_items = $this->grabMenu($result->row('id')); There is quite a bit going on but what is nice is that the "builders" i set in an array model that handles arrays and all i have to do with any multi-level array i have to build is call out to the array_model. The builder is just a iterative function while the display function is recursive... in this instance i have a a wrapper that can be set but i didnt include that function. Hope this helps out.
[eluser]skunkbad[/eluser]
I came up with what will be the best solution for my needs. I studied other recursive menus, including my own that are working, and this was the answer: Code: <?php |
Welcome Guest, Not a member yet? Register Sign In |