0

I am looping through some locations, and then calling a recursive function to get these locations categories and sub categories. What is happening that the function returns data as a combination of previous loop result. How can I get rid of this, please help me on this my code looks like this.

foreach ($data as $row) {
   $get_options = categoryWithSubcategories(0, 0,$row['location_id'],$dbConn);
   // here I am passing $row['location_id'] to this function, but it merge prvious data within next loop.
}

Recursive function is below.

function categoryWithSubcategories($current_cat_id, $count,$locationId,$dbConn)
{
    static $option_results;
    // if there is no current category id set, start off at the top level (zero)
    if (!isset($current_cat_id)) {
        $current_cat_id =0;
    }
    // increment the counter by 1
    $count = $count+1;
    // query the database for the sub-categories of whatever the parent category is
    $sql =  "SELECT cat_id, cat_name from tbl_category where cat_parent_id = $current_cat_id and locationid=$locationId and delete_flag='0'";
    $stmt =  $dbConn->prepare($sql);
    $result =$stmt->execute();
    $data = $stmt->fetchAll();
    $num_options = $stmt->rowCount();
    if ($num_options > 0) {
        foreach ($data as $categoryList) {
            // if its not a top-level category, indent it to
            //show that its a child category
            if ($current_cat_id!=0) {
                $indent_flag =  ' ';
                for ($x=2; $x<=$count; $x++) {
                    $indent_flag .=  ' >> ';
                }
            }
            $cat_name = $indent_flag.$categoryList['cat_name'];
            $option_results[$categoryList['cat_id']] = $cat_name;
            // now call the function again, to recurse through the child categories
            categoryWithSubcategories($categoryList['cat_id'], $count,$locationId,$dbConn );
        }
    }
    return $option_results;
}
2
  • have you thought about changing the sql to retrieve all the info you need? Commented Jan 28, 2017 at 12:21
  • aren't you missing a return statement? return categoryWithSubcategories($categoryList['cat_id'], $count,$locationId,$dbConn ); inside the foreach-loop Commented Jan 28, 2017 at 12:53

1 Answer 1

1

In function categoryWithSubcategories() You have defined $option_results as static. That is the reason behind results from function getting merged/added in each new iteration.

You can try this: 1. Remove static from $option_results. 2. Store the result from function categoryWithSubcategories() in below line.

categoryWithSubcategories($categoryList['cat_id'], $count,$locationId,$dbConn );

This can be written as

$option_results = array_merge(categoryWithSubcategories($categoryList['cat_id'], $count,$locationId,$dbConn), $option_results) ;
Sign up to request clarification or add additional context in comments.

10 Comments

You are right, just noticed the static variable. But the solution given by you is not working. But I got an idea that I have to play around this static variable.
Can you please clarify why it isn't working? Is it not giving the expected result or is it throwing some error?
It's throwing warning array_merge() first value is not an array.
I think this can happen if control isn't going in this if block. "if ($num_options > 0) {" . This can be solved if you define $option_results = array() in first line of this function.
Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.