简体   繁体   English

如何从 mysql / php 获取树视图中的记录

[英]How to get records in tree view from mysql / php

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 ie 123 and property_id ie 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.我有 user_id 即 123 和 property_id 即 1 我需要知道如何与我共享此属性的朋友制作一棵树,然后与我的朋友共享此 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.为此,定义一个 function 来获取用户的所有孩子,并递归调用它找到的孩子的孩子。

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.可能有一个框架可以帮助您可视化 PHP 中的树,但我不知道。 There will typically be some sort of object structure with layout properties and child objects;通常会有某种具有布局属性和子对象的 object 结构; instantiate those properties in your recursive function from the last step.在最后一步的递归 function 中实例化这些属性。

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).如果您是 A,有朋友 B 和 C,并且 C 也是 B 的朋友,B 将显示为您的朋友(在树的第 2 层)和 C 的朋友(在第 3 层)。 You'll have to check what's your desired behaviour in that spot and might have to, eg, ignore C the second time you encounter it.您必须检查您在该位置的期望行为,并且可能必须在第二次遇到它时忽略 C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM