You wish you had a database command like Oracle's "CONNECT BY" which handles this kind of thing, but apparently you don't have this sort of command. So you will have to do some PHP programming to handle the returned values.
This is a fairly simple loop:
SELECT a row
Get the id value
SELECT the children having that ID and loop...
etc.
Your challenge will be to create arrays of information from these values, and then managing the arrays. These arrays will be designed based on your program's needs, and they will be up to you to figure out. But the basic looping required for a parent-child relationship is pretty simple and a basic part of database programming.
PS there are 2 approaches to this problem: Drill down from a top component: This has the possibilty of near infinite looping. Alternate is roll up from a predefined lower level component or component level. This is a finite problem. Oracle's CONNECT BY statement is based on this second method, which is much easier to program, as you can predefine the number of columns in a returned row.
Typical use of this is a 'parts list': This part is made up these 5 smaller components, each of which is made of smaller components, etc. 1 component might appear in 200 parts, etc.
Database designers I have known call this a 'guzintaa*' relationship,
This part guzinta that part, that part guzinta another part, etc.
*guzinta == 'goes in to'