PHP / MySQL - creating a hierarchy of navigation menus

So, the last menu will look something like this:

Item B
    Item B-1
        Item B-1-2
        Item B-1-1
Item A
    SubItem A-1
    SubItem A-2
Item C

Based on the following database entries:

id        menu_title          parent_menu_id    menu_level    weight
1         Item A                0                           1                     1
2         Item B                0                           1                     0
3         Item C                0                           1                     2
4         SubItem A-2       1                           2                     1
5         Item B-1             2                           2                     0
6         Item B-1-1          5                           3                     1
7         SubItem A-1       1                           2                     0
8         Item B-1-2          5                           3                     0

How will I be displayed? My assumption is that it will store all the elements in a multidimensional array, and then scroll it in some way ...

+3
source share
5 answers

Working with the data structure, since you will often have to include recursion or several queries to build a tree.

Have you considered other ways to store a hierarchy? Check out the modified pre-workaround - here is a good PHP-based article on this .

+7

( Oracle, START WITH/CONNECT BY, ). : .

, . , , . . (), .

( ):

- :

$menu = array(
  array(
    'name' => 'Home',
    'url' => '/home',
  ),
  array(
    'name' => 'Account',
    'url' => '/account',
    'children' => array(
      'name' => 'Profile',
      'url' => '/account/profile',
    ),
  ),
  // etc
);

:

<ul class="menu">;
  <li><a href="/">Home</a></li>
  <li><a href="/account">Account Services</a>
    <ul>
      <li><a href="/account/profile">Profile</a></li>
...

PHP , . , HTML, .:)

+8

, . MySQL SQL. , , .

+2
+1

, , - .

Item B
    Item B-1
        Item B-1-2
        Item B-1-1
Item A
    SubItem A-1
    SubItem A-2
Item C

1 Item B
  1.1 Item B1
    1.1.1 Item B11
    1.1.2 Item B12
2 Item A
  2.1 Item A1
  2.2 Item B2
3 Item C

( , ), , , .

I use nested set hierarchies for more complex things that require computation, e tc, but I believe this approach served well

0
source

Source: https://habr.com/ru/post/1702209/


All Articles