How to build an html tree from a MySQL query?

I want to make a site where there will be unlimited number of categories.
For categories create a table cat, which stores all categories and their sub categories.
The cat table contains three columns: id, name, prev_id (parent). Basic (root) categories have a parent (prev_id) = 0.
The rest of the categories have corresponding id of the parent.

Get all categories from cat to a variable, and both of them continue to build a tree, I do not understand.
Anyway, I already found the article on this topic.постоение-дерева but I still don't get it.

Can anyone help to explain, verbally, to describe the algorithm of building the tree?
Do not have an example on the link you another way.
Or at least zakommentirovat code link.
June 10th 19 at 14:46
2 answers
June 10th 19 at 14:48
1. Recursive method, as you have... Its easy to build, but on reading confusing, when selecting parents or children on the contrary, constantly recursion to take will have...

2. Nested sets... through the indexes, very comfortable for reading, but a little too for the record... for a large number of categories still would recommend it as an entry in the online store less than reading
Look at where he well this algorithm explains:
the only video that I had watched :)

There are even on the framework is done (as you suggest), but the principle can be taken on Board without frame
June 10th 19 at 14:50
Here's an article for you: Hierarchical (recursive) queries

Find more questions by tags MySQLWeb DevelopmentPHPDatabases