I am trying to setup a navigation menu, but i am not getting back the Subcats. I think its in the .= but i am not sure.
In the createTree i call the create SubCat.
I think I'll look over it.
Tip?
Thmx!
id name description parent_id
1 electronics desc 0
2 cloth kleding 0
3 washing mach desc was 1
4 dryer desc droger 1
5 pants desc broek 2
$arrayMenu = array();
foreach( $navigation as $row ){
$arrayMenu[$row['id']] = array("parent_id" => $row['parent_id'], "name" => $row['name']);
}
function createSubCat($array, $curParent){
$html = '';
foreach ($array as $categoryId => $category) {
if ($curParent == $category['parent_id']) {
$html .= '<li id="' . $categoryId . '" ><a href="#">' . $category['name'] .'</a></li>';
}
return $html;
}
}
function createTree($array, $curParent) {
$html = '';
foreach ($array as $categoryId => $category) {
if ($curParent == $category['parent_id']) {
$html .= '
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $category['name'] .' <b class="caret"></b></a>
<ul class="dropdown-menu">
';
$html .= createSubCat($array, $categoryId);
$html .= '
</ul>
</li>
';
}
return $html;
}
}
$arrayss = createTree($arrayMenu, 0);
print_r($arrayss);
out put:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Electronics<b class="caret"></b></a>
<ul class="dropdown-menu">
</ul>
</li>
You return the result inside the loops : the functions end after the first iteration. It should be :
function createSubCat($array, $curParent){
$html = '';
foreach ($array as $categoryId => $category) {
if ($curParent == $category['parent_id']) {
$html .= '<li id="' . $categoryId . '" ><a href="#">' . $category['name'] .'</a></li>';
}
}
return $html;
}
function createTree($array, $curParent) {
$html = '';
foreach ($array as $categoryId => $category) {
if ($curParent == $category['parent_id']) {
$html .= '
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $category['name'] .' <b class="caret"></b></a>
<ul class="dropdown-menu">
';
$html .= createSubCat($array, $categoryId);
$html .= '
</ul>
</li>
';
}
}
return $html;
}