2

I am trying to view dynamic menu into my application. I made my policy that I will have a table and I will add menu items by myself into the table. I have a table Menu_Panel like this:

id   menu_name    parent_menu_id
1    root         0
4    user         1
5    sales        1
6    user-list    2

For nested items I made a query and Then I executed the query which outputs are below:

Array
(
[1] => Array
    (
        [id] => 1
        [menu_name] => root
        [parent_menu_id] => 0
        [sub-menu] => Array
            (
                [4] => Array
                    (
                        [id] => 4
                        [menu_name] => user
                        [parent_menu_id] => 1
                        [sub-menu] => Array
                            (
                                [6] => Array
                                    (
                                        [id] => 6
                                        [menu_name] => user-list
                                        [parent_menu_id] => 2
                                    )


                            )

                    )

                [5] => Array
                    (
                        [id] => 5
                        [menu_name] => Sales
                        [parent_menu_id] => 1
                    )

            )

    )

)

Now I think, I got my desired result cause it looks like nested. Now I want to show this recursive menu items in ul->li into my view page. Here is my code: I kept the result into $side_bar_menu::

@foreach($side_bar_menu as $module)
            <li class="mm-dropdown">

                <a href="#"><i class="menu-icon fa fa-columns"></i><span class="mm-text">User </span></a>
                <ul>
                @foreach($module['sub-menu'] as $sub_module)
                    <li>
                        <a tabindex="-1" href="{{route('menu-panel')}}"><span class="mm-text">Menu Panel</span></a>
                    </li>
                @endforeach
                </ul>
            </li>
            @endforeach

I think I miss something in foreach. in this case $module['sub-menu'] is not working. As I noticed that array indexing number are not sequential.

My question is that how may I view the item lists in my view file in php ?

2
  • Wait what do you actually want to achieve? Commented Mar 16, 2016 at 5:03
  • @TimOgilvy :: I got the answer in below. I want to view nested menu items with li -> ul -> li Commented Mar 16, 2016 at 5:08

2 Answers 2

1

I think you could use foreach from root level. Here is the view code you may try:

@if($side_bar_menu)
    <?php //print_r($side_bar_menu); exit(); ?>
    @foreach($side_bar_menu as $module)
            @foreach($module['sub-menu'] as $sub_module)
            <?php // print_r($sub_module['menu_name']);  ?>
                <li class="mm-dropdown">
                    <a tabindex="-1" href="{{route('menu-panel')}}"><span class="mm-text">{{$sub_module['menu_name']}}</span></a>
                    <ul>
                        @foreach($sub_module['sub-menu'] as $sub_sub_module)
                        <?php // print_r($sub_sub_module['menu_name']);  ?>
                            <li>
                                <a tabindex="-1" href="{{route('menu-panel')}}"><span class="mm-text">{{$sub_sub_module['menu_name']}}</span></a>
                            </li>
                        @endforeach
                    </ul>
                </li>
            @endforeach
    @endforeach

I hope this will work. Please let me know if you have any question.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks, at a glance like you are trying to treat a sequential array as an associative array. I'm not familiar with the cake/laravel markup you are using however. In vanilla PHP you would treat it like this:

foreach ($module as $moduleIndex => $menuModule) {

    // Do top level stuff here using $menuModule
    $subMenu = $menuModule['sub-menu'];

    foreach ($subMenu as $smIndex => $subItem) {

        // Do submenu stuff here using $subItem

       renderSubmenuItems($subItem);

    }
}

Not sure if this will help, but have a look into handling associative vs sequential arrays. Hopefully you will find your issue.

3 Comments

:: Its also a great solution. We can make items from our method instead of view page. I really like this one.
@SelimReza I have to confess I dislike templating engines when they tempt me away from using OOP to solve problems. But I am also showing my ignorance of Cake and Laravel
:: Indeed. would you please elaborate little more so that I would earn some knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.