I have a table People
. I want to display an HTML table consisting of every parent, with all of their children directly underneath of them.
_________
|People |_____________________________________________
|-------------------------------------------------------|
| id | parent | firstname | lastname |
|-------------------------------------------------------|
| 1 0 James Donovan |
| 2 0 Jeffrey Williams |
| 3 0 Emmit Herring |
| 4 2 Carol Williams |
| 5 2 Sarah Williams |
| 6 1 Nikolai Donovan |
|_______________________________________________________|
Expected output:
________________________________________________
|Jeffrey Williams |
|------------------------------------------------|
| - Carol Williams |
| - Sarah Williams |
|________________________________________________|
|James Donovan |
|------------------------------------------------|
| - Nikolai Donovan |
|________________________________________________|
|Emmit Herring |
|------------------------------------------------|
|________________________________________________|
How do I build an associative array containing the right result set to iterate over? I'm confused about the right SQL and the right PHP to build the final array.
Specifically, I am unsure how to display a hierarchical relationship between two MySQL tables. SQL result sets are not multidimensional as far as I know. Putting a SQL query in a for
loop is terrible for performance. So what do you do?
I guess I'm looking for an adjacency list implementation in MySQL.
This question should be easy if I could split everything out into two tables, but unfortunately I have to stick with this un-normal table structure.