I have a test Database Table by the name of autos like following
I am using a recursive function in my model to get the data in array and dumped it. It looks perfect like following
Array
(
[menu] => Array
(
[0] => Array
(
[id] => 1
[name] => Automobiles
[parent] => 0
[child] => Array
(
[0] => Array
(
[id] => 2
[name] => Honda
[parent] => 1
[child] => Array
(
[0] => Array
(
[id] => 3
[name] => Cars
[parent] => 2
[child] => Array
(
[0] => Array
(
[id] => 4
[name] => Civic
[parent] => 3
[child] => Array
(
[0] => Array
(
[id] => 5
[name] => Prosmetic
[parent] => 4
)
)
)
)
)
)
)
In my view I am creating a standard bootstrap multi-level dropdown but I am not getting all the child menus
Problem : I am not getting all the child
I believe I have found the reason which is in my view code . Following is the code snippet which renders the drop-down
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Menu<span class="caret"></span></button>
<ul class="dropdown-menu">
<?php for($i=0;$i<count($menu);$i++){?>
<?php if(!empty($menu[$i]['child'])){?>
<li class="dropdown-submenu">
<a class="test" href="#"><?php echo $menu[$i]['name']?> <span class="caret"></span></a>
<ul class="dropdown-menu">
<?php for($j=0;$j<count($menu[$i]['child']);$j++){?>
<li><a href="#"><?php echo $menu[$i]['child'][$j]['name']?></a></li>
<?php }?>
</ul>
</li>
<?php }else{?>
<li><a tabindex="-1" href="#"><?php echo $menu[$i]['name']?></a></li>
<?php }}?>
</ul>
</div>
I am only able to get First level child because I am only checking for the first level child. How can I do it repeatedly (recursively) in the view. I can't just keep checking for sub-child of a child and so on.. There has to be a way. Can anyone point me to the right direction please?
EDIT: My Model
function getCategoriesByParentId($category_id) {
$data = $this->db->select('*')->from('autos')->WHERE('parent',$category_id)->get()->result_array();
for($i=0;$i<count($data);$i++)
{
if($this->getCategoriesByParentId($data[$i]['id']))
{
$data[$i]['child']=$this->getCategoriesByParentId($data[$i]['id']);
}
}
return $data;
}
My Controller
public function index()
{
$this->load->model('Test_model');
$data['menu']=$this->Test_model->getCategoriesByParentId(0);
//echo '<pre>';print_r(($data));echo '</pre>';exit;
$data['title']='testing';
$this->load->view('head',$data);
$this->load->view('dropdown');
}
I inserted some more categories and subcategories. Now the Menu looks like this