I have a table called 'categories' with int cID, varchar name, and int parent.
Some sample data may look like this:
cID name parent
1 root 0
2 vehicles 1
3 cars 2
4 sports 3
5 trucks 2
6 2x4 5
7 4x4 5
8 sedans 3
9 bmw 8
10 porsche 4
etc...
Now, I want to be able to make a tree out of this where things are in alphabetical order and have their children indented under them like (assuming phpbuilder formats this correctly)
vehicles
cars
sedans
bmw
sports
porsche
trucks
2x4
4x4
This cID, name, parent is the most efficient way to store this heirarchial data, but makes it a bit of a trick to efficiently format.
I suppose the best way to start would be to SELECT * FROM categories ORDER BY parent, name and then dump all of that into an associative array and work on it from there.
Does anyone know of code or an algorithm (presumably recursive) that would take care of this problem efficiently? I have some ideas, but they aren't looking pretty.