0

I am trying to get a PHP function that will generate a menu div structure.

I am using Meekrodb to connect to MySQL.

Meekrodb returns data to an array

$menu_results = DB::query("SELECT mi.id, mi.display, mi.parent_item, mi.sort
     FROM ibtstl_menu_items mi
     WHERE mi.state = 1 AND mi.fk_menu = 1
     ORDER BY mi.sort");

So the $menu_results array has data:

Array
(
    [0] => Array
        (
            [id] => 1
            [display] => Repertoire
            [parent_item] => 0
            [sort] => 1.0000
        )

    [1] => Array
        (
            [id] => 9
            [display] => Search repertoire
            [parent_item] => 1
            [sort] => 1.0100
        )

    [2] => Array
        (
            [id] => 3
            [display] => Add new chart
            [parent_item] => 1
            [sort] => 1.0500
        )

    [3] => Array
        (
            [id] => 4
            [display] => Add new composer / lyricist
            [parent_item] => 1
            [sort] => 1.0501
        )

    [4] => Array
        (
            [id] => 5
            [display] => Add new tag
            [parent_item] => 1
            [sort] => 1.0502
        )

    [5] => Array
        (
            [id] => 6
            [display] => Reports
            [parent_item] => 1
            [sort] => 1.0800
        )

    [6] => Array
        (
            [id] => 7
            [display] => Public repertoire list
            [parent_item] => 6
            [sort] => 1.0801
        )

    [7] => Array
        (
            [id] => 8
            [display] => Gigs
            [parent_item] => 0
            [sort] => 3.0000
        )

    [8] => Array
        (
            [id] => 11
            [display] => Search gigs
            [parent_item] => 8
            [sort] => 3.0010
        )

    [9] => Array
        (
            [id] => 10
            [display] => Add new gig
            [parent_item] => 8
            [sort] => 3.0020
        )

    [10] => Array
        (
            [id] => 2
            [display] => Videos
            [parent_item] => 0
            [sort] => 5.0000
        )

    [11] => Array
        (
            [id] => 12
            [display] => Search videos
            [parent_item] => 2
            [sort] => 5.0010
        )

    [12] => Array
        (
            [id] => 13
            [display] => Add new video
            [parent_item] => 2
            [sort] => 5.0200
        )

)

I've looked at dozens of answers here and other websites but I just can't seem to make it make sense.

It might be two parted: I might need to process this array so that the children items are arrays in their parent items? I saw that was an approach out there. And then I need a recursive function to process that out so I can have

<div>
  <a href="#">Repertoire</a>
  <div>
    <a href="#">Search repertoire</a>
    <a href="#">Add new chart</a>
    <a href="#">Add new composer / lyricist</a>
    <a href="#">Add new tag</a>
    <a href="#">Reports</a>
    <div>
      <a href="#">Public repertoire list</a>
    </div>
  </div>
  <a href="#">Gigs</a>
  <div>
    <a href="#">Search gigs</a>
    <a href="#">Add new gig</a>
  </div>
  <a href="#">Videos</a>
  <div>
    <a href="#">Search videos</a>
    <a href="#">Add new video</a>
  </div>  
</div>

Any help would be appreciated!

Thanks

2 Answers 2

0

Cosidering $menu_results array as mentioned in your query

echo "<div>";
$sunsubmenucount = 0;
foreach ($menu_results as $menu_results_key => $parent) 
{

    if($parent['parent_item']==0)
    {

        echo"<a href='#'>".$parent['display']."</a>";
        echo"<div>";
        foreach ($menu_results as $submenu_key => $submenu_value) 
        {
            if($submenu_value['parent_item']==$parent['id'])
            {
                echo"<a href='#'>".$submenu_value['display']."</a>";

                foreach ($menu_results as $sub_submenu_key => $sub_submenu_value) 
                {

                        if($sub_submenu_value['parent_item'] == $submenu_value['id'] && array_search($submenu_value['id'], array_column($menu_results, 'parent_item')))
                        {

                            if($sunsubmenucount==0)
                            {
                                echo"<div>";
                            }
                            $sunsubmenucount ++;
                               echo"<a href='#'>".$sub_submenu_value['display']."</a>";
                        }

                }
                if($sunsubmenucount!=0)
                {
                              echo"</div>";
                              $sunsubmenucount = 0;
                }

            }

        }
        echo"</div>";

    }

}
echo "</div>";

Hope this will help.

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

1 Comment

thanks very much. Your code did produce the exact menu as I indicated in my question - but, if I added a fourth (fifth, sixth...) layer to my menu structure, they weren't displayed as well... I can't quite work out why not but your code seemed to just show 3 levels....
-1

The answer I eventually found was here: https://stackoverflow.com/questions/10782810/echo-menu-tree-with-recursive-function

Add ChildCount to the select

select Category.*, (select count(distinct c1.id) from Category as c1 where c1.root = Category.id) as ChildCount from Category
function recurse($categories, $parent = null, $level = 0)
{
    $ret = '<ul>';
    foreach($categories as $index => $category)
    {
        if($category['root'] == $parent)
        {
            $ret .= '<li><a href="#"><p class="Tier' . $level . '">' . $category['name'] . '</p></a>';
            if($category['ChildCount'] > 0)
                $ret .= $this->recurse($categories, $category['id'], $level+1);
            $ret .= '</li>';
        }
    }
    return $ret . '</ul>';
}

Changing the <UL> to <div> and removing the <LI> and it worked a treat.

Recursively, so could add more layers in my menu (if desired) and they displayed automatically.

2 Comments

Please add all information to your answer instead of linking to other questions
@NicoHaase Done!

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.