dqd54099 2015-04-30 06:22
浏览 48
已采纳

Nestable List Plugin PHP MYSQlL Json Tree结构

my first question. I am using Nestable List Plugin and trying to get same structure from an database.

The database looks like this (data source) (tobad need 10 reputation) This type of structure

ID: 1 ParentID: 0 Text: Hello World
Type: Text ----New row ID: 2 ParentID: 1 Text: CHild :D Type: text

and so on., dont know the name for it but thats one type of hierarchy structure. I am trying to convert that into something like this, with unlimited lvls: [{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]}]

This is my try so far

PHP Side

if($productSQL[0])
{  
    foreach($productSQL[1]['obj_ID'] as $iIDKey => $iIDValues) 
    { 

        if($productSQL[1]['objProductIDArr'][$iIDKey] == 0)
        {
            // Type Text
            $objData = $getDataInspection->listTemplate((object) ['resellerID' => $calcObj['resellerID'],'action' => 'objectByID','objectID' => $productSQL[1]['ii_object_id'][$iIDKey]]);
            $type = "text";

           // Push values into arrays
           array_push($idArr, $productSQL[1]['obj_ID'][$iIDKey]);
           array_push($textArr, $objData[0]['Value'][0]);
           array_push($contentType, $type);
           array_push($parentIDArr, $productSQL[1]['obj_parentID'][$iIDKey]);   

        }
        else
        {
            // Type Table
            $type = "table";
        }

    }

    $jsonHolder['id'] = $idArr;
    $jsonHolder['text'] = $textArr;
    $jsonHolder['parentID'] = $parentIDArr;
    $jsonHolder['type'] = $contentType;       

    $test = $functions->buildTree($jsonHolder);

    print_r($test);
}
else
{
    // No data present
    $json = "empty";
}

Save them into arrays and the save them into single array again and then putting it further to buldTree function that I tried to implement from Veerendra but without succsess PHP - How to build tree structure list?

Function

// Got this function from Stackoverflow :), thanks Veerendra :=
function buildTree(array $elements, $parentId = 0) {
    $branch = array();
    foreach($elements['id'] as $elementsKey => $elementValues) 
    {        
        if ($elements['parentID'][$elementsKey] == $parentId) 
        {

            $jsonArr['id'] = $elementValues;  
            $jsonArr['parentID'] = $elements['parentID'][$elementsKey];  
            $jsonArr['text'] = $elements['text'][$elementsKey];  
            $jsonArr['type'] = $elements['type'][$elementsKey]; 

            $children = $this->buildTree($elements, $elementValues);
            if ($children) 
            {
                $elements['children'] = $jsonArr;
            }

            $branch[] = $jsonArr;
        }
    }
    return $branch;
}

This is the result so far from $branch

Array
(
    [0] => Array
        (
            [id] => 219
            [parentID] => 0
            [text] => butik
            [type] => text
        )

    [1] => Array
        (
            [id] => 244
            [parentID] => 0
            [text] => kontor
            [type] => text
        )

)

Hope this question makes any sence?, I can explain further afcorse, just ask. Thanks

  • 写回答

1条回答 默认 最新

  • dragon7713 2015-05-01 04:32
    关注

    Okay, I got it thanks to --> Pierre de LESPINAY https://stackoverflow.com/a/22020668/1912618

    if($productSQL[0])
    {  
        foreach($productSQL[1]['obj_ID'] as $iIDKey => $iIDValues) 
        { 
            if($productSQL[1]['objProductIDArr'][$iIDKey] == 0)
            {
                // Type Text
                $objData = $getDataInspection->listTemplate((object) ['resellerID' => $calcObj['resellerID'],'action' => 'objectByID','objectID' => $productSQL[1]['ii_object_id'][$iIDKey]]);
                $type = "text";
                $arr[] = ['id' => $productSQL[1]['obj_ID'][$iIDKey], 'parentid' => $productSQL[1]['obj_parentID'][$iIDKey], 'name' => $objData[0]['Value'][0]];
            }
            else
            {
                // Type Table
                $type = "table";
    
            }
        }
        $tree = $functions->createTree($arr);
        print_r($tree);
    }
    else
    {
        // No data present
        $json = "empty";
    }
    
    
    // Class
        /* Recursive branch extrusion */
        function createBranch(&$parents, $children) {
            $tree = array();
            foreach ($children as $child) {
                if (isset($parents[$child['id']])) {
                    $child['children'] =
                        $this->createBranch($parents, $parents[$child['id']]);
                }
                $tree[] = $child;
            } 
            return $tree;
        }
    
        /* Initialization */
        function createTree($flat, $root = 0) {
            $parents = array();
            foreach ($flat as $a) {
                $parents[$a['parentid']][] = $a;
            }
            return $this->createBranch($parents, $parents[$root]);
        }
    

    This code give me solution to create unlimited lvls and unlimited parents that works Nestable List Plugin. To create an JSON from the array use json_encode($tree,JSON_UNESCAPED_UNICODE);

    :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?