views:

586

answers:

3

I have a function which gets the ids of all children of a parent from my DB. So, if I looked up id 7, it might return an array with 5, 6 and 10. What I then want to do, is recursively find the children of those returned ids, and so on, to the final depth of the children.

I have tried to write a function to do this, but I am getting confused about recursion.

function getChildren($parent_id) {
    $tree = Array();
    $tree_string;
    if (!empty($parent_id)) {
        // getOneLevel() returns a one-dimentional array of child ids
        $tree = $this->getOneLevel($parent_id);
        foreach ($tree as $key => $val) {
            $ids = $this->getChildren($val);
            array_push($tree, $ids);
            //$tree[] = $this->getChildren($val);
            $tree_string .= implode(',', $tree);
        }

        return $tree_string;
    } else {
        return $tree;
    }

}//end getChildren()

After the function is run, I would like it to return a one-dimentional array of all the child ids that were found.

+2  A: 

Nested Set Model instead of Adjacency List Model


Can I suggest that you store your nodes in your database under NSM instead of ALM?

Notice that with ALM, (which is what you are using) getting the children nodes is quite difficult, its possible, but requires extra work. If you use nested set model selecting a child node, or all nodes, or even finding the depth of all nodes can be done in a single SQL query.

I hope this sheds some light on how you could solve your problem, if you are still young in the development of your project switching now will save you a lot of headaches later.

Laykes
With adjacency list model, is it easy to update/move the parent?
Nic Hubbard
No, it takes quite a few queries to update ALM tables. Around6~ depending on how well you can optimise your queries. The benefits are all made when you SELECT. Then rather than doing 4 queries selecting to get depth, children, parents etc, you can do it just one query. So if you select more than you insert and update, you should use ALM imho.
Laykes
Isn't it just the other way around? With nested sets it's dead easy to retrieve the complete tree (or any subset of the tree), by selecting and sorting on the left values associated with each node.Refer to http://dev.mysql.com/tech-resources/articles/hierarchical-data.html for more information (MySQL specific, but should be easily portable to another RDBMS if needed).
wimvds
Sorry, yeah, you are right. It is the other way around. I shall edit. :) I always get them mixed up.
Laykes
I figured it out, and for now, am sticking with NMS, as all I really need was this function to do a number of things.
Nic Hubbard
A: 

This work fine for me:

function getOneLevel($catId){
    $query=mysql_query("SELECT categoryId FROM categories WHERE categoryMasterId='".$catId."'");
    $cat_id=array();
    if(mysql_num_rows($query)>0){
        while($result=mysql_fetch_assoc($query)){
            $cat_id[]=$result['categoryId'];
        }
    }   
    return $cat_id;
}

function getChildren($parent_id, $tree_string=array()) {
    $tree = array();
    // getOneLevel() returns a one-dimensional array of child ids        
    $tree = $this->getOneLevel($parent_id);     
    if(count($tree)>0 && is_array($tree)){      
        $tree_string=array_merge($tree_string,$tree);
    }
    foreach ($tree as $key => $val) {
        $this->getChildren($val, &$tree_string);
    }   
    return $tree_string;
}

Call the getChildren(yourid); Then it will return the complete array of children for that given node/parent.

Deep Gupta
A: 

Rather than array_push($tree, $ids); try $tree = array_merge($tree, $ids);. Kill the $tree_string .= implode(',', $tree); and just return $tree. (Once)

function getChildren($parent_id) {
    $tree = Array();
    if (!empty($parent_id)) {
        $tree = $this->getOneLevel($parent_id);
        foreach ($tree as $key => $val) {
            $ids = $this->getChildren($val);
            a$tree = array_merge($tree, $ids);
        }
    }
    return $tree;
}
Josh