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 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)