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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化