PHP MySql: Print Tree - Parent Child Checkbox

后端 未结 1 1894
栀梦
栀梦 2021-01-25 09:51

I have This MySql table for put multiple news categories(parent/child) :

ID  |   NAME    |  PARENTID  | DESC |
 1  |   LINUX   |     0      | NULL |         


        
1条回答
  •  耶瑟儿~
    2021-01-25 10:19

    Use recursion! Note: the code below is not safe for cyclic graphs (nodes may not be ancestors of themselves)!

    printChildren($items,0);
    function printChildren(array $items, $parentId){
        foreach($items as $item){
            if($item['parent']==$parentId){
                print '
  • '; print $item['label']; //or whatever you want about the current node print '
      '; printChildren($items, $item['id']); print '
  • '; } } }

    0 讨论(0)
提交回复
热议问题