I have a php page pulling data from a sql2000 stored procedure representing the BOM (Bill of Material) for a product. Right now it’s displaying as a flat table, and works great. However, after 3 days of googling/hacking, I can’t for the life of me figure out how tranform that data into an indented collapsable/expandable table or UL.
Table is as follows:
- ID[<–unique]
- BOM_Level
- Parent_Item
- Component_Item
- Component_qty
The BOM_Level is exactly that, the level of the BOM (a number between 0-12). I was assuming because that was provided it would be simple, in that the bom_level really represents the level of indentation of each part, but alas, it has not been simple….
The general structure is as follows, but the number of parts and levels will vary by product:
0 Top Level
1 - Assembly 1
2 - part 1 of Assembly 1
2 - part 2 of Assembly 1
2 - part 3 of Assembly 1
2 - part 4 of Assembly 1
1 - Assembly 2
2 - Assembly 1 of Assembly 2
3 - part 1 of Assembly 1 of Assembly 2
3 - part 2 of Assembly 1 of Assembly 2
3 - part 3 of Assembly 1 of Assembly 2
I’ve been trying to do something like:
- If the bom_level is == previous_bom_level then create li
- If the bom_level is > previous_bom_level add a new UL and start new li
- If the BOM level is < previous bom level, wrap up the previous UL and start another set of li.
With all that being said, I just can’t quite wrap my head around how to do this. Any ideas?
Suppose you have data like this:
Where
comp_item_nois unique, andparent_item_nopoints to it’s parent. Withparent_item_no=0being a root node.You can map this to another structure with:
Which in turn can be recursively traversed, with something like this:
to produce a nested ul. Example (for the data from above):
Demo: http://codepad.org/FJwX3Z1c