1

I'm using Opencart. Categories are stored in a category table with primary key category_id and a field parent_id that signifies the parent category.

Given a category_id I wont to be able to update the price of all products by a certain percentage.

If I can get a list of all the category_ids that are the sub categories or sub-sub categories or sub-sub-sub, etc. it should be fairly straight forward to use these to run the price updates.

I know there's a lot of examples of building php array tree structures with recursive functions similar to this

public function getTree($parent_category_id) {
    $arr = array();
    $sql = "select * from bs_category where parent_id = '$parent_category_id'";
    $query = $this->db->query($sql);
    foreach ($query->rows as $row) {
       $arr[] = array(
         "Parent" => $row["category_id"],
         "Children" => $this->getTree($row["category_id"])
       );
     }
     return $arr;
  }

but this gives me a tree structure of the form

Array
(
    [0] => Array
        (
            [Parent] => 568
            [Children] => Array
                (
                    [0] => Array
                        (
                            [Parent] => 571
                            [Children] => Array
                                (
                                    [0] => Array
                                        (
                                            [Parent] => 572
                                            [Children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

Whereas all I want is a simple list eg.

array(568, 571, 572)

I can't seem to get a simple list of all the category_ids without weird duplicates, etc... tbh recursions all blows my mind a bit...

Any help much appreciated.

5
  • how do you exit recursion? Also how do you pass results to other functions? You don't have it in your argument list Commented Dec 15, 2015 at 14:54
  • So what's the problem here.. the function seems to be ok.. you just need to call it with $categories = $this->getTree(0); Commented Dec 15, 2015 at 15:01
  • Problem is this gives me a tree structure and I just want a simple list / array (see edit above), thanks Commented Dec 15, 2015 at 15:10
  • So you want to get an array of all of the parent id's recursively? Commented Dec 15, 2015 at 15:15
  • yeah, well an array of all the category_ids, i guess at the bottom level, they will be parents without any child categories... so yeah. Commented Dec 15, 2015 at 15:16

1 Answer 1

3

You seem to want a simple one-dimensional array (eg. a list) with the sub-category-ids, what you're currently building is a tree (nested arrays). This should to the trick:

public function getList($parentId)
{
    $retVal = array($parentId);

    $query = "SELECT * FROm `bs_category` WHERE `parent_id` = ".$parentId;
    $result = $this->db->query($query);

    foreach($result->rows as $row)
    {
        $retVal = array_merge($retVal, $this->getList($row["category_id"]));
    }

    return $retVal;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's the one... I've added $this-> to the recursive function call.... Thank you.

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.