简体   繁体   English

如何使用PHP中的数组从数据库中基于n级生成带有父子的图形树?

[英]How to generate graphically tree with parent and child based on n level from database using array in PHP?

I have following kind of data to table:我有以下类型的数据要表:

id parent_id child_id level id parent_id child_id 级别
1 53987 52548 1 1 53987 52548 1
2 60764 52548 2 2 60764 52548 2
3 60764 53987 1 3 60764 53987 1
4 60764 59695 2 4 60764 59695 2
5 63457 59695 1 5 63457 59695 1
6 60764 63457 1 6 60764 63457 1

So, how i can get data by recursively with level and store data to array like ['child_id','parent_id',level].那么,我如何通过递归获取数据并将数据存储到像 ['child_id','parent_id',level] 这样的数组中。 I need help for writting query and generate the tree.我需要帮助编写查询并生成树。

The tree should be like :这棵树应该是这样的: 在此处输入图片说明

Note: I can't change the database's table structure.注意:我无法更改数据库的表结构。 I must need tree based on given table structure.我必须需要基于给定表结构的树。

Try This Query:试试这个查询:

select  id,
        `child_id`,
        `parent_id` , level
from    (select * from vtiger_ib_level
         order by parent_id, id) products_sorted,
        (select @pv := '60764') initialisation
where   find_in_set(`parent_id`, @pv)
and     length(@pv := concat(@pv, ',', id))

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

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