Chewy
Chewy

Reputation: 196

Weird Return Value Logic Error from PHP Recursion

I need to write a recursive function to search through a set of parent and children link headers, then get the names of embedded key values per header. Ex. Clothing->Men->Shoes. Now, each category has an unknown number of values attached to them. I have a function that can successfully echo all of these values recursively through the parents. But when I try to get the return value from the function, it's missing some, and I can't understand why : /.

Code is below

public function getFamilies($cat){
            $objCurrentCategory = Category::Load($cat); // creates a QCodo object of the passed category ID.

        $str_Query = "SELECT DISTINCT p.family
                      FROM xlsws_product p, xlsws_product_category_assn pc
                      WHERE p.rowid=pc.product_id
                      AND pc.category_id=".$cat; // sql query to retrieve all Families relating to this category.

        $objFamilyDb = Family::GetDatabase(); // retrieves the QCodo database object for Family to execute queries against.
        $objFamilies = Family::InstantiateDbResult($objFamilyDb->Query($str_Query)); // executes the query and saves the result.

        foreach($objFamilies as $family){ // for each family returned, get the family name and add it to the array of names.
            if ($family->Family !== ""){
                $families [] = $family->Family;
            }
        }

        if ($objCurrentCategory->ChildCount > 0){ // if current category has children, create a list of all children rowids.
            $str_Query = "SELECT rowid FROM xlsws_category
                          WHERE parent=".$objCurrentCategory->Rowid; // query to get all children of the category.
            $objChildCategoriesDb = Category::GetDatabase(); // retrieves the QCodo database object for Category to execute queries against.
            $objChildCategories = Category::InstantiateDbResult($objChildCategoriesDb->Query($str_Query)); // executes the query and saves the result.

            foreach($objChildCategories as $child){ // passes through the children to get their families.
                //$families [] = KG::getFamilies($child->Rowid);
                $childFam = KG::getFamilies($child->Rowid);
            }
        }

        $compiled = KG::compileFamilies($childFam); // helper function, not important.


        foreach($compiled as $compile){
            $families[] = $compile;
        }

        foreach($families as $familyt){ // this echo statement correctly displays all names.
            //echo ":".$familyt."<br />";
        }

        return $families;
    }

So this displays all the names through echo to the screen during the functions run, and the array WITHIN the function can be echo'd to the screen as well. But when I try to return all of the names from returned result in another page, I'm missing a bunch of names.

EDIT It looks like the $families variable is not persisting through each recursive call to getFamilies, and is only returning the children at the end, instead of the children and all their parents.

Upvotes: 0

Views: 120

Answers (1)

Chewy
Chewy

Reputation: 196

Fixed. problem was I was trying to clean up the array in the same recursive statement and it was throwing everything off since the cleaning "helper method" was being returned and not the full associative array of families. Sigh @ me for being oblivious.

Upvotes: 0

Related Questions