F
F
foobic2018-04-16 16:01:36
JavaScript
foobic, 2018-04-16 16:01:36

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

3 answer(s)
T
TCloud, 2018-04-16
@OTCloud

Working with trees includes mandatory work with recursion, otherwise you are threatened with -100500 points to karma.

V
Vitaly, 2018-04-16
@rim89

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

A
Alexander Pryadko, 2018-04-17
@alexander_v_pryadko

No recursion, only nested set model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question