Dynamic menu php bootstrap mysql
<p>I have a problem with this code, And I can have the 1st level, On the  second level I have an id number and the 3eme level nothing.</p>

<p>I need to find informations on 4 levels of deep</p>

<p>Also the code produce an error : <code>Illegal string offset 'label'</code> on this line :</p>

<pre><code>foreach($menu_sub[$submenus['sub_menu']] as $sub2_key =&gt; $submenus2) {
</code></pre>

<p>result</p>

<pre><code>index
---- 8
--------- not appear
--------- not appear
---- 3
---- 2
---- I

Configuration
---- 9
--------- not appear
--------- not appear
----1
----1
----M
----1

Catalogue
</code></pre>

<p>The DB</p>

<pre><code>CREATE TABLE `administrator_menu` (
  `id` int(11) NOT NULL,
  `link` mediumtext NOT NULL,
  `parent_id` int(11) NOT NULL,
  `sort_order` int(11) NOT NULL,
  `class` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `administrator_menu`
--

INSERT INTO `administrator_menu` (`id`, `link`, `parent_id`, `sort_order`, `class`) VALUES
(1, '', 0, 2, ''),
(3, '', 0, 1, ''),
(5, '', 0, 3, ''),
(6, '', 0, 4, ''),
(7, '', 3, 1, ''),
(8, '', 3, 2, ''),
(9, '', 1, 1, ''),
(10, '', 9, 0, '');

ALTER TABLE `administrator_menu`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `administrator_menu`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;    

CREATE TABLE `administrator_menu_description` (
  `id` int(11) NOT NULL,
  `label` varchar(255) NOT NULL,
  `language_id` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `administrator_menu_description`
--

INSERT INTO `administrator_menu_description` (`id`, `label`, `language_id`) VALUES
(3, 'Accueil', 1),
(7, 'Administration', 1),
(7, 'Administration', 2),
(5, 'Catalog', 2),
(5, 'Catalogue', 1),
(1, 'Configuration', 1),
(1, 'Configuration', 2),
(10, 'Configuration générale', 1),
(10, 'general Configuration', 2),
(3, 'Index', 2),
(8, 'Index Catalogue', 1),
(8, 'Index Shop', 2),
(9, 'Ma boutique', 1),
(9, 'My shop', 2);    

ALTER TABLE `administrator_menu_description`
  ADD PRIMARY KEY (`id`,`language_id`),
  ADD KEY `label` (`label`);

ALTER TABLE `administrator_menu_description`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
</code></pre>

<p>the script</p>

<p>result of sql request</p>

<pre><code>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 
</code></pre>

<p>Note :
parent_id is the number of id selected to create an hierarchical menu
For example the <code>id = 10</code> and <code>parent_id = 9</code> , we are on the 3em level<br>
For example the <code>id = 9</code> and <code>parent_id = 1</code> , we are on the 2em level<br>
For example the <code>id = 1</code> and <code>parent_id = 0</code> , we are on the 1st level</p>

<pre><code>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

&lt;?php
  // Select all entries from the menu table
  $Qmenus = $Db-&gt;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-&gt;bindInt(':language_id', $Language-&gt;getId());
  $Qmenus-&gt;execute();

  $Qmenus = $Qmenus-&gt;fetchAll();
?&gt;
&lt;!-- Navigation --&gt;
&lt;nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0"&gt;
  &lt;div class="navbar-default sidebar" role="navigation"&gt;
    &lt;div class="sidebar-nav navbar-collapse"&gt;
      &lt;ul class="nav" id="side-menu"&gt;

&lt;?php
  $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'];
        $menu_sub[$menus['id']] = $menus;
      } else if (isset($menu_sub[$menus['parent_id']])) {
        $menu_sub[$menus['parent_id']]['sub_menu'] = $menus['id'];
        $menu_sub[$menus['id']] = $menus;
      }
    }
  }

  foreach($menu_parent as $key =&gt; $menus) {
    echo '&lt;li&gt;&lt;a href="#"&gt;&lt;i class="fa fa-sitemap fa-fw"&gt;&lt;/i&gt;' . $menus['label'] . '&lt;span class="fa arrow"&gt;&lt;/span&gt;&lt;/a&gt;';

    if (!empty($menus['sub_menu'])) {
      echo '&lt;ul class="nav nav-second-level"&gt;';
        foreach($menu_sub[$menus['sub_menu']] as $sub_key =&gt; $submenus) {
          echo '&lt;li&gt;&lt;a href="#"&gt;' . $submenus['label'] . '&lt;/a&gt;';
          if (!empty($submenus['sub_menu'])) {

            foreach($menu_sub[$submenus['sub_menu']] as $sub2_key =&gt; $submenus2) {

              echo '&lt;li&gt;&lt;a href="#"&gt;' . $submenus2['label'] . '&lt;/a&gt;';

              if (!empty($submenus2['sub_menu'])) {

                foreach($menu_sub[$submenus2['sub_menu']] as $sub3_key =&gt; $submenus3) {
                  echo '&lt;li&gt;&lt;a href="#"&gt;' . $submenus3['label'] . '&lt;/a&gt;&lt;/li&gt;';
                }
              }
              echo '&lt;/li&gt;';
            }
          }
          echo '&lt;/li&gt;';
        }
      echo '&lt;/ul&gt;';
    }
    echo '&lt;/li&gt;';
  }
?&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;!-- /.sidebar-collapse --&gt;
  &lt;/div&gt;
  &lt;!-- /.navbar-static-side --&gt;
&lt;/nav&gt;
</code></pre>