鍍金池/ 問答/人工智能  PHP  數(shù)據(jù)庫/ php怎么計算輸出數(shù)據(jù)表里組織架構圖的數(shù)組結構

php怎么計算輸出數(shù)據(jù)表里組織架構圖的數(shù)組結構

我的數(shù)據(jù)庫表user_tree是這樣的,這是一個左右值樹,Level代表樹深度,Lft是左值,Rgt是右值。

userid name Lft Rgt Level
1001 wang 1 16 1
1002 li 2 7 2
1003 wlm 8 15 2
1005 bander 3 4 3
1008 qipl 5 6 3
1007 buliao 9 12 3
1009 wumao 13 14 3
1013 zhangsan 10 11 4

組織結構圖:
組織結構圖

用php怎么能實現(xiàn),任意給一個userid值,就能組織成一個多維數(shù)組結構。
例如我輸入一個函數(shù)aaa(userid=1003),就能返回1003下面所有子孫節(jié)點,組織的多維數(shù)組。只用一次查詢,不能循環(huán)去數(shù)據(jù)庫查詢,服務器受不住。最多2次查詢,就能出現(xiàn)結果。最多返回4層。例如,輸入1003。查詢到1003,在2層,那么最多查詢到6層。

查詢1003節(jié)點的所有子孫節(jié)點是這樣的,左右值樹參考

SELECT * FROM user_tree WHERE Lft >= 8 AND Lft <= 15 AND Level<=6 ORDER BY Level ASC
array(
    'userid' => '1003',
    'name' => 'wlm',
    'children' => array(
        0 => array(
            'userid' => '1007',
            'name' => 'buliao',
            'children' => array(
                0 => array(
                    'userid' => '1013',
                    'name' => 'zhangsan'
                )
            ) ,
        1 => array(
            'userid' => '1009',
            'name' => 'wumao'
            ) ,
        ) ,
    ) ,
);

想了3天了,搞不出來,所以求助一下這里的前輩。

回答
編輯回答
兔寶寶

數(shù)據(jù)庫查出來不可能是多維以上的結果,查出來后你必須按照左右權值計算出tree來,可以在加個字段pid 就簡單了

2017年12月7日 09:09