KnpLabs / KnpMenu

Menu Library for PHP

Home Page:https://knplabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fill menu with nested data from a database table

mgsmus opened this issue · comments

I have an array like this:

/*
Main
- Corporate
-- About Us
-- Meet the Team
- Services
*/
$data = array(
            array(
                'id'        => 1,
                'title'     => 'Main',
                'url'       => '#',
                'left_id'   => 1,
                'right_id'  => 10,
                'depth'     => 0,
                'parent_id' => 0,
            ),
            array(
                'id'        => 2,
                'title'     => 'Corporate',
                'url'       => '#',
                'left_id'   => 2,
                'right_id'  => 7,
                'depth'     => 1,
                'parent_id' => 1,
            ),
            array(
                'id'        => 3,
                'title'     => 'Services',
                'url'       => '/services',
                'left_id'   => 8,
                'right_id'  => 9,
                'depth'     => 1,
                'parent_id' => 1,
            ),
            array(
                'id'        => 4,
                'title'     => 'About Us',
                'url'       => '/about-us',
                'left_id'   => 3,
                'right_id'  => 4,
                'depth'     => 2,
                'parent_id' => 2,
            ),
            array(
                'id'        => 5,
                'title'     => 'Meet the Team',
                'url'       => '/meet-the-team',
                'left_id'   => 5,
                'right_id'  => 6,
                'depth'     => 2,
                'parent_id' => 2,
            ),
        );

How can I fill a menu with that data?

@mgsmus - I made a GIST for you. normalizeDbResult() function was added to make the GIST flexible to other peoples needs as well. You can also create several menus with the builder library. Beyond that I also added the ability to have up to 5 levels(non recursive for performance reasons) of Menus and you can easily add more. Build a KNPMenu from a Database or Array

maybe we could start a cookbook section in the doc with some code examples?