PHP/MySQL - building a nav menu hierarchy(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
基于以下数据库记录:
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.
您是否考虑过其他存储层次结构的方法?查看修改后的预购遍历 - 这是一篇不错的关于此的基于 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 - 构建导航菜单层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP/MySQL - 构建导航菜单层次结构
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
