Yoko Yoko - 1 month ago 10
PHP Question

lost in loop foreach

I am trying to create a dynamic menu with sub level but I think I am lost in my loop. I can have the 1st level but after other level has the same name.

What is the modifications that I can applied

Thank you.

result : we can see at the 2bnd level the same name for all 1st level.

Accueil
2nd level Administration
2nd level Index Catalogue
Catalogue
2nd level Administration
2nd level Index Catalogue
Configuration
2nd level Administration
2nd level Index Catalogue



<?php
// Select all entries from the menu table
$Qmenus = $Db->prepare('SELECT a.id,
a.link,
a.parent_id,
a.class,
a.sort_order,
amd.label
FROM :table_administrator_menu a,
:table_administrator_menu_description amd
where a.id = amd.id
and amd.language_id = :language_id
ORDER BY a.parent_id,
a.sort_order
');

$Qmenus->bindInt(':language_id', $Language->getId());
$Qmenus->execute();

$Qmenus = $Qmenus->fetchAll();
?>

<!-- Navigation -->
<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-default sidebar" role="navigation">
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<?php
foreach ($Qmenus as $menus) {
if ($menus['parent_id'] == 0) {
?>
<li>
<a href="#"><i class="fa fa-sitemap fa-fw"></i><?php echo $menus['label']; ?><span class="fa arrow"></span></a>
<?php
foreach ($Qmenus as $menus) {
if ( $menus['parent_id'] == 1) {
?>
<ul class="nav nav-second-level">
<li>
<a href="#">2nd level <?php echo $menus['label']?></a>
</li>
<?php
foreach ($Qmenus as $menus) {
if ( $menus['parent_id'] == 2) {
?>
<li>
<a href="#">Third Level <span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li>
<a href="#">Third Level Item</a>
</li>
<li>
<a href="#">Third Level Item</a>
</li>
<li>
<a href="#">Third Level Item</a>
</li>
<li>
<a href="#">Third Level Item</a>
</li>
</ul>
</li>
<?php
}
} // end level 3
?>

</ul>
<?php
} //
} // end level 3
?>
</li>
<?php
} // end level 1
}
?>


</ul>
</div>
<!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->
</nav>


more information

Array
(
[0] => Array
(
[id] => 3
[link] =>
[parent_id] => 0
[class] =>
[sort_order] => 1
[label] => Accueil
)

[1] => Array
(
[id] => 5
[link] =>
[parent_id] => 0
[class] =>
[sort_order] => 1
[label] => Catalogue
)

[2] => Array
(
[id] => 1
[link] =>
[parent_id] => 0
[class] =>
[sort_order] => 2
[label] => Configuration
)

[3] => Array
(
[id] => 7
[link] =>
[parent_id] => 1
[class] =>
[sort_order] => 1
[label] => Administration
)

[4] => Array
(
[id] => 8
[link] =>
[parent_id] => 1
[class] =>
[sort_order] => 2
[label] => Index Catalogue
)


table administrator_menu

id parent_id sort_order class
1 0 2
3 0 1
5 0 3
6 0 4
7 3 1
8 3 2
9 1 1
10 9 0


table description menu

id lable language_id
1 Configuration 1
1 Configuration 2
3 Accueil 1
3 Index 2
5 Catalogue 1
5 Catalog 2
7 Administration 1
7 Administration 2
8 Index Catalogue 1
8 Index Shop 2
9 Ma boutique 1
9 My shop 2
10 Configuration générale 1
10 general Configuration 2

Answer

Previous examples were didn't do what you expected, so i made new online on fiddle, click to see!:

$menu_parent = array();
$menu_sub = array();
foreach ($Qmenus as $menus) {
    if ($menus['parent_id']==0) {
        $menu_parent[$menus['id']] = $menus;
        }
    else {
        if (isset($menu_parent[ $menus['parent_id'] ])) {
            $menu_parent[ $menus['parent_id'] ]['sub_menu'][$menus['id']] = $menus['id'];
            $menu_sub[$menus['id']] = $menus;
            }
        else if (isset($menu_sub[ $menus['parent_id'] ])) {
                $menu_sub[ $menus['parent_id'] ]['sub_menu'][$menus['id']] = $menus['id'];
                $menu_sub[$menus['id']] = $menus;
                }
        }
}

foreach($menu_parent as $key => $menus) {
        echo '<li><a href="#"><i class="fa fa-sitemap fa-fw"></i>'.$menus['label'].'<span class="fa arrow"></span></a>';
        if (!empty($menus['sub_menu'])) {
            echo '<ul class="nav nav-second-level">';
            foreach($menus['sub_menu'] as $second_level) {
                    echo '<li><a href="#">'.$menu_sub[ $second_level ]['label'].'</a>';
                    if (!empty($menu_sub[ $second_level ]['sub_menu'])) {
                        echo '<ul class="nav nav-third-level">';
                        foreach($menu_sub[ $second_level ]['sub_menu'] as $third_level) {
                                echo '<li><a href="#">'.$menu_sub[ $third_level ]['label'].'</a>';
                                if (!empty($menu_sub[ $third_level ]['sub_menu'])) {
                                    echo '<ul class="nav nav-fourth-level">';
                                    foreach($menu_sub[ $third_level ]['sub_menu'] as $fourth_level) {
                                            echo '<li><a href="#">'.$menu_sub[ $fourth_level ]['label'].'</a></li>';
                                            }
                                    echo '</ul>';
                                    }
                                echo '</li>';
                                }
                        echo '</ul>';
                        }

                    echo '</li>';
                    }
            echo '</ul>';
            }
        echo '</li>';
}