PHP/MySQL - building a nav menu hierarchy

前端 未结 5 554
予麋鹿
予麋鹿 2021-01-01 05:24

So the final 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
         


        
5条回答
  •  -上瘾入骨i
    2021-01-01 05:48

    Another simple way you can generate hierarchy if you don't want to use nested sets is to use a simple text string in front.

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

    Would become

    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
    

    The digit in front of each item could be stored in a field and parsed based on length (representing the depth of where it is) to tell you everything you need to know about where it goes.

    I use nested set hierarchies for more complicated stuff that requires calculation,e tc, but I find this approach has served well

提交回复
热议问题