Jump to content

Parent / Sub Parent -> Multi Array


formsim

Recommended Posts

Hey guys,

Been programming for a while but finally ran into a problem i can't figure out.

 

I got a mysql table with data like follows.

 

ID - PARENT ID - ITEM NAME

1  -      0        - Physical Container 1

2  -      0        - Physical Container 2

3  -      2        - Box Inside Container 2

4  -      2        - Another Box Inside Container 2

5  -      3        - 1 Item Inside Box Inside Container 2

6  -      3        - 2 Item Inside Box Inside Container 2

7  -      2        - Item Inside Physical Container 2

 

Don't laugh, I didn't create the table, this is how the data is being gathered and stored sadly.

What i'm trying to do is create a menu tree style from an array as follows.

 

- Physical Container 1

+ Physical Container 2

      + Box Inside Container 2

            - 1 Item Inside Box Inside Container 2

            - 2 Item Inside Box Inside Container 2

      - Another Box Inside Container 2

      - Item Inside Physical Container 2

 

 

I've tried multiple nested loops to create the parent interface arrays and try to reassemble into 1 multi array to export to my front end code but all my logic has been flawed. Closest i have gotten is the menu system only 1 parent interface deep. Then my code is completely lost with what to do next.

 

Any suggestions by the experts?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/177970-parent-sub-parent-multi-array/
Share on other sites

The downside to an adjaceny list model is that you must write a recursive function (involving multiple queries to the database) if you do not already know the depth beforehand.

 

Check out this article on SitePoint.  If it does not help, let me know and I'll help you out.

And didn't even know it had a name  ;D

 

sadly the mutli query solution is what i've been leaning towards as my last option, but the list is sometimes 100-150 items deep and i just wouldn't sit right know its hitting the db that many times in the background. Let me take a look at your link since it seems to nail my problem on the head.

 

Big thankx

 

 

 

If you want the easy way out without using a recursive function and multiple queries..

http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/

 

There are disadvantages to this approach though, such as it is much more difficult to find a specific node.

This nailed my problem head on. Being able to add children to a parent deep inside a tree.

This looks like it would work but it sounds like your somewhat against the practice of using references to solve this purpose.

 

Is there a downside that you are aware of?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.