Sandeep Thakkar
Sandeep Thakkar

Reputation: 94

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 : enter image description here

Note: I can't change the database's table structure. I must need tree based on given table structure.

Upvotes: 1

Views: 366

Answers (1)

Mahesh Kathiriya
Mahesh Kathiriya

Reputation: 662

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))

Upvotes: 1

Related Questions