Sort and Format Menu correctly
<p>i have a problem. I want to create my navigation (main menu) froom the database.</p>

<p>It works well and also with unlimited parent items. The only problem is the formatting.</p>

<p>The output should look like that:</p>

<pre><code>&lt;li class="sub"&gt;
    &lt;a class="widgets" href="#"&gt;Einstellungen&lt;/a&gt;
    &lt;ul&gt;
        &lt;li class="sub"&gt; 
            &lt;a href="#"&gt;Allgemeines&lt;span&gt;6&lt;/span&gt;&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=""&gt;test1&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt; 
        &lt;li&gt;&lt;a href=""&gt;test2&lt;/a&gt;&lt;/li&gt;             
    &lt;/ul&gt;
&lt;/li&gt;   
</code></pre>

<p>I think you can see the structure behind this.</p>

<p><strong>array:</strong></p>

<pre><code>Array
(
    [1] =&gt; Array
        (
            [parent_id] =&gt; 0
            [name] =&gt; Hauptseite
            [children] =&gt; Array
                (
                )

        )

    [2] =&gt; Array
        (
            [parent_id] =&gt; 0
            [name] =&gt; Einstellungen
            [children] =&gt; Array
                (
                    [3] =&gt; Array
                        (
                            [parent_id] =&gt; 2
                            [name] =&gt; Allgemeines
                            [children] =&gt; Array
                                (
                                    [4] =&gt; Array
                                        (
                                            [parent_id] =&gt; 3
                                            [name] =&gt; test1
                                            [children] =&gt; Array
                                                (
                                                )

                                        )

                                )

                        )

                    [5] =&gt; Array
                        (
                            [parent_id] =&gt; 2
                            [name] =&gt; test2
                            [children] =&gt; Array
                                (
                                )

                        )

                )

        )

)
</code></pre>

<p><strong>UPDATE:</strong></p>

<pre><code>function buildList($testArray) {

    echo '&lt;ul&gt;';
    foreach ($testArray as $id =&gt; $menuItem):
        if (isset($menuItem['parent_id']) &amp;&amp; $menuItem['parent_id'] == 0): // check if it's a parent category
            echo '&lt;li class="home"&gt;&lt;a class="widgets" href="#"&gt;' . $menuItem['name'] . ' (' . $id . ')' . '&lt;/a&gt;';
            if (isset($menuItem['children'])): // if it has children let's spit them out too
                echo '&lt;ul class="child"&gt;';
                outputChildren($menuItem['children']);
                echo '&lt;/ul&gt;';

            endif;

        endif;


        if (isset($menuItem['parent_id']) &amp;&amp; $menuItem['parent_id'] != 0): // if it's a child let's get it's info
            echo '&lt;li class="sub"&gt;&lt;a href="#"&gt;' . $menuItem['name'] . '&lt;/a&gt;';
            if (isset($menuItem['children'])): // if there are grandchildren let's get them too
                echo '&lt;ul class="child"&gt;';

                outputChildren($menuItem['children']);
                echo '&lt;/ul&gt;';
            endif;
            echo '&lt;/li&gt;';
            continue;
        endif;

        if (isset($menuItem['parent_id']) &amp;&amp; $menuItem['parent_id'] == 0): // check if it's a parent category
            echo '&lt;/li&gt;';
        endif;
    endforeach;
    echo '&lt;/ul&gt;';
}

function outputChildren($child) {

    if (isset($child['name'])):
        echo '&lt;li class="sub"&gt;&lt;a href="#"&gt;' . $child['name'] . '&lt;/a&gt;';
        if (!empty($child['children'])):

            echo '&lt;ul&gt;';
            outputChildren($child['children']);
            echo '&lt;/ul&gt;';
        endif;
        echo '&lt;/li&gt;';
    endif;
}

$sites = array();

$sql = 'SELECT menu_id, menu_name, parent_id FROM `' .$_config['prefix']. '_menu`';

$result = $db-&gt;query($sql);
while ($row = $result-&gt;fetch_assoc()) {
    $sites[(int) $row['menu_id']] = array(
        'parent_id' =&gt; (int) $row['parent_id'],
        'name' =&gt; $row['menu_name'],
        'children' =&gt; array()
    );
}

foreach ($sites as $site_id =&gt; $site) {
    $sites[$site['parent_id']]['children'][$site_id] = &amp; $sites[$site_id];
}

$sites = $sites[0]['children'];
print_r($sites);


echo buildList($sites);
</code></pre>

<p><strong>Will output:</strong></p>

<pre><code> &lt;ul&gt;
     &lt;li class="home"&gt;
         &lt;a class="widgets" href="#"&gt;Hauptseite (1)&lt;/a&gt;
         &lt;ul class="child"&gt;&lt;/ul&gt;
     &lt;/li&gt;
     &lt;li class="home"&gt;
         &lt;a class="widgets" href="#"&gt;Einstellungen (2)&lt;/a&gt;
         &lt;ul class="child"&gt;&lt;/ul&gt;
     &lt;/li&gt;
 &lt;/ul&gt;
</code></pre>

<p>See the first example how it should look like.</p>

<p>Thanks</p>
