I'm about to give 100 bounty points for answer to this question
So I have very difficult question about recursions - how to get all items count of category and all childs that contains that parent and more deeper until the end?
I have table:
+----+---------------+-----------------+
| id | category name | category_parent |
+----+---------------+-----------------+
| 1 | cars | 0 |
+----+---------------+-----------------+
| 2 | real estate | 0 |
+----+---------------+-----------------+
| 3 | clothes | 0 |
+----+---------------+-----------------+
| 4 | bmw | 1 |
+----+---------------+-----------------+
| 5 | audi | 1 |
+----+---------------+-----------------+
| 6 | 100 | 5 |
+----+---------------+-----------------+
| 7 | 80 | 5 |
+----+---------------+-----------------+
| 8 | A4 | 5 |
+----+---------------+-----------------+
| 9 | QUATRO | 8 |
+----+---------------+-----------------+
| 10 | TDI | 8 |
+----+---------------+-----------------+
| 11 | Black | 9 |
+----+---------------+-----------------+
| 12 | White | 9 |
+----+---------------+-----------------+
| 13 | 2 doors | 11 |
+----+---------------+-----------------+
| 14 | 5 doors | 11 |
+----+---------------+-----------------+
and my products table look like this:
+----+---------------+-----------------+
| id | category_id | name |
+----+---------------+-----------------+
and for example I want to count all items that are in cars
category. So basically I should pass this category id (1)
and somehow make a recursion to count all items. But I have no idea how to deal with it, becouse childs of that category could be unlimited.
So when I want to know all items of that parent count, I should make something like this:
1++:
4++
5++:
6++
7++
8++:
9++:
11++:
13++
14++
12++
10++
I hope you will understand what I need and give me any suggestion that could help me.
also, this is the begining I have made so far - I could implement it but in the future I will stuck in recursion... so its worth nothing.
public function get_category_tree_id_list($cat_id, $list_array = FALSE)
{
if ( !$list_array ){
$items = $this->system->_getCustomTableData('categories', array(array('category_parent' => $cat_id)), 'id DESC');
$this->__tmp['id_list'] = [];
foreach ( $items as $key => $value ) {
$this->__tmp['id_list'][] = $value['id'];
}
}
}
0 comments:
Post a Comment