I am trying to make a category tree which accepts n count of sub-categories for each category. I got some code from the net and modified it for my CI but I cannot get the exact result that I want.
I have very simple DB:
id, parent_id, title
If parent_id = 0 then it's main category, if not then it's sub-category of the saved one.
This is the method I have
public function all_categories($parent_id = 0, $categoriesArray = '', $i = 0) {
if(!is_array($categoriesArray))
$categoriesArray = array();
$allCategories = $this->category_model->get_all(['parent_id' => $parent_id]);
foreach($allCategories as $category) {
$categoriesArray[$i] = $category;
$categoriesArray[$i][] = $this->all_categories($category['id'], $categoriesArray);
}
return $categoriesArray;
}
It sorts the categories pretty good - I get main cat and every sub- sub- sub- category of each children but I get them in one-dimensional array:
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 0
[title] => Shoes
[description] => This is the shoes main category
[image] =>
[created_at] => 2016-02-18 14:22:56
[updated_at] => 2016-02-18 14:22:56
[active] =>
)
[1] => Array
(
[id] => 2
[parent_id] => 1
[title] => Sub-shoes
[description] => This is sub-category of shoes
[image] =>
[created_at] => 2016-02-18 14:27:44
[updated_at] => 2016-02-18 14:27:44
[active] =>
)
[2] => Array
(
[id] => 8
[parent_id] => 2
[title] => Sub-sub-shoes
[description] => This is sub-sub-shoes category
[image] =>
[created_at] => 2016-02-21 13:32:44
[updated_at] => 2016-02-21 13:32:44
[active] =>
)
[3] => Array
(
[id] => 5
[parent_id] => 1
[title] => Sub-shoes 2
[description] => This is sub-shoes category after deleting the first of it's kind
[image] =>
[created_at] => 2016-02-18 14:40:33
[updated_at] => 2016-02-18 14:40:33
[active] =>
)
[4] => Array
(
[id] => 3
[parent_id] => 0
[title] => Dresses
[description] => This is the main dress category
[image] =>
[created_at] => 2016-02-18 14:33:41
[updated_at] => 2016-02-18 14:33:41
[active] =>
)
Can you show me a way so I can order them as each main category has index 'subs' which is array containing all its children and so on.
Best regards!