recursive function to get all the child categories

后端 未结 7 857
时光取名叫无心
时光取名叫无心 2020-12-03 03:54

Here is what I\'m trying to do: - i need a function that when passed as an argument an ID (for a category of things) will provide all the subcategories and the sub-sub categ

7条回答
  •  生来不讨喜
    2020-12-03 04:36

    function categoryChild($id)
    {
        $s = "SELECT category_id,name FROM proads_categories WHERE parent_id =".$id;    
        $r = mysql_query($s);
        $children = array();
        if(mysql_num_rows($r) > 0)
        {
            #It has children, let's get them.
            while($row = mysql_fetch_array($r))
            {          
                #Add the child to the list of children, and get its subchildren
                $children[$row['category_id']]['nam'] = $row['name'];
                $arr = categoryChild($row['category_id']);
                if(count($arr) > 0)
                {
                    $children[$row['category_id']]['child'] = categoryChild($row['category_id']);
                } 
            }
        }
        return $children;
    }
    

    It's perfect. If you need please try this

提交回复
热议问题