简体   繁体   中英

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
1 53987 52548 1
2 60764 52548 2
3 60764 53987 1
4 60764 59695 2
5 63457 59695 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]. 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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