Quantcast
Channel: Php recursive method getting products - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Php recursive method getting products

$
0
0

At this moment i am trying to get all products from the children of the specified category recursively.

I am using laravel and created a class to handle this in App/Helpers so it will be autoloaded.

The structure of the database is as followed Categories :

1|0|2015-07-28 14:45:38|2015-07-28 14:45:382|1|2015-07-28 14:52:43|2015-07-28 14:56:47

Products :

1|2|2015-07-28 15:10:45|2015-07-29 11:04:44

So the product has a categoy id of 2. Category 2 had parent id of 1. So if the user is on the page of category 1 he/she needs to see the products belonging to category 1 and category two because 2 is a child of category 1.

This is my current method :

 public static function getProducts($id) {        self::$products =  DB::table('products')->where('category_id', $id )->get();        $categories = DB::table('categories')->where('parent_id', $id)->get();        if ($categories !== null) {            foreach ($categories as $category) {                array_merge(self::$products, self::getProducts($category->id) );            }        }         return self::$products;    }

I hope someone can help me to retrieve all products that belong to the child categories recursively.

Update:

I have installed the Laravel debug toolbar and found out that my queries don't return any result when loading the web page. When i query it with my sql client i do get the proper result.

This is my current method to get all products from subcategories:

public static function getProducts($id)    {        $products = [];        $categories = DB::table('categories')->where('parent_id', $id)->get();        array_merge($products, DB::table('products')->where('category_id', $id)->get());        if ($categories !== null) {            foreach ($categories as $category) {                self::getProducts($category->id);            }            return $products;        }    }

Database schema:

CREATE TABLE "products" ("id" integer not null primary key autoincrement, "url" text not null, "title" text not null, "keywords" text not null, "description" text not null, "content" text not null, "attributes" text not null, "pdf" text not null, "image" text not null, "category_id" integer not null, "created_at" datetime not null, "updated_at" datetime not null);CREATE TABLE "categories" ("id" integer not null primary key autoincrement, "url" text not null, "title" text not null, "keywords" text not null, "description" text not null, "content" text not null, "parent_id" integer not null, "created_at" datetime not null, "updated_at" datetime not null);

Also opened up the debug bar and site =>http://remotefix.nl:81/producten/flenzen

If anyone can help me this would be appreciated.


Viewing all articles
Browse latest Browse all 2

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>