PHP MySql: Print Tree - Parent Child Checkbox

后端 未结 1 1875
栀梦
栀梦 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 '<li>';
                print $item['label']; //or whatever you want about the current node
                print '<ul>';
                printChildren($items, $item['id']);
                print '</ul></li>';
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题