dsgffz2579 2019-07-07 10:35
浏览 92
已采纳

too long

I want to build a tree view made of departments(parent) and sub-departments(child). In my database I think I have a good structure which is like this:

--------------------------------------------
 Dep_name         | dep_id | dep_parent_id    
--------------------------------------------
 Accounting       |    1   |        0
 Human-Resources  |    2   |        0
 IT               |    3   |        0
 Network          |    4   |        3
 Web Development  |    5   |        3
 Front-End        |    6   |        5
 Back-End         |    7   |        5

Departments which have the dep_parent_id 0 ---> they do not have a parent department. For example, Web Development and Network are children of IT department. Front End and Back End are children of Web Development.

I have found a recursive function which is appropriate to get all the data from this database table and put them on an array in te right structure. But the problem is that I dont know how to display this array like a tree view. Like this for example

  • Departments
    • Accounting
    • Human Resources
    • IT
    • Network
    • Web Development
      • Front End
      • Back End

..... and so on ....

In my database I think I have a good structure which is like this:

enter image description here

I have tried to print the array in a very simple way using

print_r($tree);

And it prints it like this:

Array (
    [0] => stdClass Object (
        [Dep_name] => Accounting and Finance
        [dep_id] => 1
        [dep_parent_id] => 0
    )
    [1] => stdClass Object (
        [Dep_name] => Human-Recources
        [dep_id] => 2
        [dep_parent_id] => 0
    )
    [2] => stdClass Object (
        [Dep_name] => IT
        [dep_id] => 3
        [dep_parent_id] => 0
        [children] => Array (
            [0] => stdClass Object (
                [Dep_name] => Network
                [dep_id] => 5
                [dep_parent_id] => 3
            )
            [1] => stdClass Object (
                [Dep_name] => Web Development
                [dep_id] => 6
                [dep_parent_id] => 3
                [children] => Array (
                    [0] => stdClass Object (
                        [Dep_name] => Front-End
                        [dep_id] => 7
                        [dep_parent_id] => 6
                    )
                    [1] => stdClass Object (
                        [Dep_name] => Back-End
                        [dep_id] => 8
                        [dep_parent_id] => 6
                    )
                )
            )
        )
    )
    [3] => stdClass Object (
        [Dep_name] => Marketing
        [dep_id] => 4
        [dep_parent_id] => 0
        [children] => Array (
            [0] => stdClass Object (
                [Dep_name] => web-marketing
                [dep_id] => 9
                [dep_parent_id] => 4
            )
        )
    )
)

This is my function that gets data from database table from array $data and builds a tree array $branch.

function buildTree(array $data, $parentId = 0) 
{
    $branch = array();

    foreach ($data as $element) 
    {
        if ($element->dep_parent_id == $parentId) 
        {
            $children = buildTree($data, $element->dep_id);
            if ($children) 
            {
                $element->children = $children;
            }
            $branch[] = $element;
        }

    }

    return $branch;
}

And than I print it using :

print_r(buildTree($data));

I would be very grateful if you would help me to solve this and display a tree view structure in html from array $branch that I return from function buildTree($data).

  • 写回答

2条回答 默认 最新

  • dongmeiba6151 2019-07-07 11:00
    关注

    You need a recursive function that checks if the children property of the object exists - and if it does, print out a new <ul> tag before looping those elements.

    function printTree($array) {
        $output = "<ul>
    ";
        foreach ($array as $a) {
            $output .= "<li>".$a->Dep_name."</li>
    ";
    
            if (isset($a->children)) {
                $output .= printTree($a->children);
            }
        }
        $output .= "</ul>
    ";
        return $output;
    }
    

    This will return a single string that has the HTML in the hierarchy described. The output will be like this (well, not indented like this, but the HTML will print just the same)

    <ul>
        <li>Accounting and Finance</li>
        <li>Human-Recources</li>
        <li>IT</li>
        <ul>
            <li>Network</li>
            <li>Web Development</li>
                <ul>
                    <li>Front-End</li>
                    <li>Back-End</li>
                </ul>
        </ul>
        <li>Marketing</li>
        <ul>
            <li>web-marketing</li>
        </ul>
    </ul>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面