Answer the question
In order to leave comments, you need to log in
How to build a category tree based on products?
I need to build a category tree based on the products in a table.
There is a table with products added by the user (wishlist_products) (wishlist_id, product_id)
There is a table with categories, where each category has a parent (parent_id). The main category (to which all the others are linked) has id = 0
The category output format looks like this:
Array
(
[category_id] => 3
[parent_id] => 2
[name] => Фэтбайки
)
//например в таблице есть только один продукт, у которого категория Фэтбайки(id = 3)
$categories = $this->category->getCategoriesByProducts($product_ids);
В результате:
[0] => 3 // категория Фэтбайки
$this->category->getParentsCategories(3) // получаем категорию 3 и ее родителей в виде:
[0] => 1 // Спорт
[1] => 2 //Велосипеды
[2] => 3 //Фэтбайки
Array
(
[0] => Array ( // Главная категория дочерней категории.
[cat_id] => 1
[text] => Спорт
[nodes] => Array ( //Тут Второй уровень категории
[0] => Array (
[cat_id] => 2
[text] => Велосипеды
[nodes] => Array ( //Тут Третий уровень категории
[0] => Array (
[cat_id] => 3
[text] => Фэтбайки
[nodes] =>
)
)
)
)
)
)
Answer the question
In order to leave comments, you need to log in
adjacency list to help you:
https://habrahabr.ru/post/46659/xandeadx.ru/blog/php/199
https
://www.codesd.com/item/recursive-php-function...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question