如何获取所有 children 类别并将它们显示为 mySql 中的树?

How to get all children of category and show them like tree in mySql?

我在 mySql 中有一个树系统 table。

categories table:

category_idcategory_title category_parent_id
1 Technologies 0
2 Web 1
3 OS 1
4 Software 1
5 Hardware 1
6 PHP 2
7 HTML 2
8 JavaScrip 2
9 Jquery 8
10 Angulra.js 8

如您所见,我有一个树系统,我想要一个 PHP 函数或一个 SQL 查询 return 所有 children 如下所示:

Technologies
Technologies > Web
Technologies > OS
Technologies > Software
Technologies > Hardware
Technologies > Web > PHP
Technologies > Web > HTML
Technologies > Web > JavaScript
Technologies > Web > JavaScript > Jquery
Technologies > Web > JavaScript > Amgular.js

你能帮忙吗?

function list_to_tree($list, $pk = 'category_id', $pid = 'category_parent_id', $child = '_child', $root = 0)
{
    $tree = array();
    if (is_array($list))
    {
        $refer = array();
        foreach ($list as $key => $data)
        {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data)
        {
            $parentId = $data[$pid];
            if ($root == $parentId)
            {
                $tree[] =& $list[$key];
            }
            else
            {
                if (isset($refer[$parentId]))
                {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
    }
    return $tree;
}