Answer the question
In order to leave comments, you need to log in
Listing directories as a tree without the help of recursion?
Hello, I'm having a problem displaying product categories for my online store.
There is a table with categories like:
id | name | parentId
1 computers 0
2 laptops 1
3 tablets 1
4 clothes 0
5 jeans 4
....
Category nesting is unlimited. So, is it possible to make the output of this table in the form of a tree, without the help of recursion?
Answer the question
In order to leave comments, you need to log in
Working with trees includes mandatory work with recursion, otherwise you are threatened with -100500 points to karma.
1. Find out the maximum parentId
2. Loop from zero to parentId collect a new array
- first the zero level
- then already nested in it
3. Output a new array
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question