PHP / MySQL的 - 建立一个导航菜单的层次结构 [英] PHP/MySQL - building a nav menu hierarchy

查看:211
本文介绍了PHP / MySQL的 - 建立一个导航菜单的层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以最终的菜单将是这个样子:

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

根据以下DB记录:

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 would I go about displaying? My guess is it'll involve storing all the items into a multidimensional array, then looping through it somehow...

推荐答案

与数据结构时,你拥有它通常会涉及到递归或多个查询来构建树。

Dealing with the data structure as you have it will often involve recursion or multiple queries to build the tree.

你有没有考虑存储层次结构的其他方式?查看修改pre-序遍历 - 这里的这个一个不错的基于PHP的文章。

Have you considered other ways of storing a hierarchy? Check out modified pre-order traversal - here's a nice PHP based article about this.

这篇关于PHP / MySQL的 - 建立一个导航菜单的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆