I have to create a tree view from the records of my table below
id user_id friend_id property_id
1 123 321 1
2 123 456 1
3 456 909 1
4 909 222 1
I have the user_id i.e 123 and property_id i.e 1 I need to know how can I make a tree with friends with whom I share this property, and afterwards with users with whom my friends share this ID.
Ok since there are several steps I’ll start out on a high level. If you need help with any of those, ask again!
First of all, you’ll need the “root” nodes, ie those users who don’t appear as children in the friend column.
Then, for each of these users, start polling all their children. For this, define a function that gets all the children of a user and which calls itself recursively for the children of the child it finds.
This is pretty abstract, because the question is what you want to do with this structure. This depends on the last part, your presentation layer. That’s what I know least about; there may be a framework which helps you visualize trees in PHP, but I don’t know. There will typically be some sort of object structure with layout properties and child objects; instantiate those properties in your recursive function from the last step.
As a side node, it isn’t clear if the data structure you obtain is actually a tree. If you are A, have friends B and C, and C is also a friend of B, B will show up as your friend (on level 2 of the tree) and as C’s friend (on level 3). You’ll have to check what’s your desired behaviour in that spot and might have to, e.g., ignore C the second time you encounter it.