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 - 构建导航菜单层次结构


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01