I'm storing some hierarchical data in MySQL. For various reasons, I've decided to use closure tables (instead of nested sets, adjacency lists, and the like). It's been working great for me so far, but now I'm trying to figure out how to actually display this tree in HTML (i.e. with correct indentations).
As an example, let's say I have a tree like so...
- Food
- Fruits
- Apples
- Pears
- Vegetables
- Carrots
- Fruits
My "Foods" table would look like this...
[ID] [PARENT_ID] [NAME]
1 0 Food
2 1 Fruits
3 1 Vegetables
4 2 Apples
5 2 Pears
6 3 Carrots
My "Closure" table would then look like this...
[PARENT] [CHILD] [DEPTH]
1 1 0
2 2 0
3 3 0
4 4 0
5 5 0
6 6 0
1 2 1
1 3 1
1 4 2
1 5 2
1 6 2
2 4 1
2 5 1
3 6 1
Now I'm wondering how I would be able to display this correctly in HTML, ideally like this...
<ul>
<li>Food
<ul>
<li>Fruits
<ul>
<li>Apples</li>
<li>Pears</li>
</ul>
</li>
<li>Vegetables
<ul>
<li>Carrots</li>
</ul>
</li>
</ul>
</li>
</ul>
...which would display my tree in bullet form as it is in the beginning of my question. Anyways, any help would be much appreciated!
Charles