Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Nested Set Tree as HTML List
  • Hi,
    I'm trying to create an HTML list (UL) out of a nested set tree, to create a menu from it.
    After spending 6+ hours and spinning my head I still couldn't find a good solution.
    I Googled every where without any luck.

    I've put the code (very dirty code still) for anyone to have a look and advise, please.
    http://bin.fuelphp.com/snippet/view/Hh

    thanks,
  • HarroHarro
    Accepted Answer
    You need to use recursion to do this properly.

    Start by passing the tree root to the method. In the method, open the UL, fetch all children, and for every child, create an LI. If the child is not a leaf node, don't close with /LI, but recurse first, inserting a nested UL for that node. And so on...
  • Thanks for the tip! "Recursion" is the magic word I was looking for.

    I found an nice article that had what I need:
    http://www.elated.com/articles/php-recursive-functions/

    The other wrong thing I was doing is using the $tree->descendants() instead of $tree->children()

    Here's the final result for anyone in need:
    http://bin.fuelphp.com/snippet/view/Hj

    Thank you!


Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion