0

i have a problem. I want to create my navigation (main menu) froom the database.

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

The output should look like that:

<li class="sub">
    <a class="widgets" href="#">Einstellungen</a>
    <ul>
        <li class="sub"> 
            <a href="#">Allgemeines<span>6</span></a>
            <ul>
                <li><a href="">test1</a></li>
            </ul>
        </li> 
        <li><a href="">test2</a></li>             
    </ul>
</li>   

I think you can see the structure behind this.

array:

Array
(
    [1] => Array
        (
            [parent_id] => 0
            [name] => Hauptseite
            [children] => Array
                (
                )

        )

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

                                        )

                                )

                        )

                    [5] => Array
                        (
                            [parent_id] => 2
                            [name] => test2
                            [children] => Array
                                (
                                )

                        )

                )

        )

)

UPDATE:

function buildList($testArray) {

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

            endif;

        endif;


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

                outputChildren($menuItem['children']);
                echo '</ul>';
            endif;
            echo '</li>';
            continue;
        endif;

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

function outputChildren($child) {

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

            echo '<ul>';
            outputChildren($child['children']);
            echo '</ul>';
        endif;
        echo '</li>';
    endif;
}

$sites = array();

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

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

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

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


echo buildList($sites);

Will output:

 <ul>
     <li class="home">
         <a class="widgets" href="#">Hauptseite (1)</a>
         <ul class="child"></ul>
     </li>
     <li class="home">
         <a class="widgets" href="#">Einstellungen (2)</a>
         <ul class="child"></ul>
     </li>
 </ul>

See the first example how it should look like.

Thanks

user1766080
  • 591
  • 3
  • 6
  • 12

1 Answers1

0

Update

Here is our test Array:

$test = Array([1] => Array(
            'parent_id' => 0,
            'name' => Hauptseite,
            'children' => Array()),
            [2] => Array(
            'parent_id' => 0,
            'name' => Einstellungen,
            'children' => Array([3] => Array(
                                    'parent_id' => 2,
                                    'name' => 'Allgemeines',
                                    'children' => Array([4] => Array(
                                                    'parent_id' => 3,
                                                    'name' => 'test1',
                                                    'children' => Array())
                                                    )
                                            ),
                                [5] => Array(
                                    'parent_id' => 2,
                                    'name' => 'test2',
                                    'children' => Array()
                                            )
                                )
                )

        );

In order to get the desired results we will take the following steps:

1) If the parent Id = 0 it's a parent (else it's a child)

2) If isset($item['children']) then we need to spit out children (and possibly grand children)

Here is the code:

function buildList($testArray) {

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

            endif;

        endif;


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

                outputChildren($menuItem['children']);
                echo '</ul>';
            endif;
            echo '</li>';
            continue;
        endif;

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

function outputChildren($child) {

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

            echo '<ul>';
            outputChildren($child['children']);
            echo '</ul>';
        endif;
        echo '</li>';
    endif;
}

You can change the echo statement's to $htmm .=

pzirkind
  • 2,338
  • 2
  • 20
  • 25
  • Thanks but wont work. Now its missing two links. See the first post for the changes i did. thanks – user1766080 Jan 20 '13 at 18:43
  • Basicly its missing "Allgemeines" which is a subitem from "EInstellungen", and its missing "test1" which is a subitem from "Allgemeines". – user1766080 Jan 20 '13 at 18:49
  • @user1766080 for that you can use "recursion" to go through each "child" see this answer: http://stackoverflow.com/questions/14095301/flatten-indexed-array/14095741#14095741 – pzirkind Jan 21 '13 at 15:29
  • @user1766080 if the answer is not clear, let me know and i will try and explain – pzirkind Jan 21 '13 at 15:31
  • Thanks for your answer. I read it like 3 times now but i cant understand. It seems very complicated. THe other thing that makes it hard to understand for me is that im from austria. So my english is not the best. Would be nice if you could help me out. Thanks for your time. – user1766080 Jan 21 '13 at 15:39
  • Thanks but i have some problems with it. At the first function there is a "for" which you dont close. WHere should i close it? at the end of the function? and at the second function there is a "endoreach;" and a "endforeach;". But there is only one for in the second function. So why there are 2 end´s? sry if im completly dump :/ – user1766080 Jan 21 '13 at 19:03
  • I also saw that you have the "return $html;" in the second function, but ater the return you still cotinue with adding code to $html. That makes no sense for me. THanks – user1766080 Jan 21 '13 at 19:04
  • @user1766080 sorry was having problems with the code editing etc.. the point was to create a function outside of your foreach loop where you can check for sub-sub-category's – pzirkind Jan 21 '13 at 19:13
  • @user1766080 this function outside the foreach checks whether the value is an array, if it is, it knows there are sub children so it calls itself again. after that it outputs the necessary value – pzirkind Jan 21 '13 at 19:14
  • Sry but i still dont really understand :/ In the second function you try to add values to $ht,l. But in this function you cant access the value from the other function. I dont know if im completly wrong or just dumb. Sorry. – user1766080 Jan 21 '13 at 19:49
  • @user1766080 no problem, i will try to update the answer later tonight (after work :) ). – pzirkind Jan 21 '13 at 20:18
  • Thanks but now it dont show any subitems. Only the items with parent id 0. That thing is really tricky -.-. Thanks man – user1766080 Jan 21 '13 at 22:48
  • @user1766080 please post a sample array of data (so that i can code it properly – pzirkind Jan 21 '13 at 22:51
  • Okay wait a moment i will do. Btw. i found the probem why it doesnt showed the sub items. But now theres another problem. See first post. – user1766080 Jan 21 '13 at 22:54
  • Np :) will look at it later – pzirkind Jan 21 '13 at 23:50
  • Hey i just wanted to ask yo, if you have some news? Or do you need more time? Thanks – user1766080 Jan 22 '13 at 20:21
  • Thanks, but still wont work as expected :( That "little" thing is a really hard thing. I updated my post with the new details. The main problem is that the html output is broken. Thanks man for your time. Michael – user1766080 Jan 22 '13 at 21:26
  • @user1766080 fixed, try code now, p.s. you can change the names of the class names to whatever you want – pzirkind Jan 22 '13 at 21:31
  • Thanks again. But still not workin. See the first post how it should look loke (without the "test2" items because its not in the test array), and how it now looks like. – user1766080 Jan 22 '13 at 21:48
  • Still problems. See first post. – user1766080 Jan 22 '13 at 22:06
  • @user1766080 sorry posted wrong code (was testing the wrong thing) will post new code soon – pzirkind Jan 22 '13 at 22:12
  • Its no problem man. I really appreciate it how much time you spend into that, to help me. No stress :) I really hope that "we" (^^) can get it to work. Many thanks again! – user1766080 Jan 22 '13 at 22:13
  • Very close. Just some little bugs as it looks like. I updated the output. – user1766080 Jan 22 '13 at 22:46
  • @user1766080 is this output using my test array, or your real array? – pzirkind Jan 22 '13 at 22:52
  • Your test array. Should i use my real array? – user1766080 Jan 22 '13 at 22:57
  • @user1766080 yes and spit out the output above – pzirkind Jan 22 '13 at 23:00
  • What you mean with split out the output? – user1766080 Jan 22 '13 at 23:02
  • Okay updated. Now you can see the complete Script. Output also updated. thanks – user1766080 Jan 22 '13 at 23:06
  • Please var_dump your array so we can fix accordingly – pzirkind Jan 22 '13 at 23:25
  • @user1766080 just to make sure...is this your actual list (see the new test array i put in answer), if it is let me know and i'll try to solve the problem for you – pzirkind Jan 23 '13 at 22:04
  • Hm. Your array looks a bit different? YOu have a array in the array as i can see? The actual array i use is in my question. If i got your question let it me know. Thanks! – user1766080 Jan 23 '13 at 22:10
  • @user1766080 i copied the array from your question and just formatted it so that you can see the parent/child relationship, if it's different please repost – pzirkind Jan 23 '13 at 22:14
  • Then its correct if you just copied it. YOu also have the function which create the array in the question. If its easier to fix it by modifie the array generation we can do that aswell. But i dont have an better idea how to do it. THanks – user1766080 Jan 23 '13 at 22:28
  • @user1766080 i won't have time to help with this problem until late next week :( what i suggest is to repost your question with the updated code, your array, and how you want it to output. i'm sure someone will be able to help you out sooner. – pzirkind Jan 24 '13 at 16:18
  • Okay i will do that. If not then iwait for you.No problem. thanks – user1766080 Jan 24 '13 at 16:36