【发布时间】:2013-05-08 21:01:06
【问题描述】:
我有一个这样的主题表:
id
title
parent_id
full_path
full_path 用于查找父级递归。像这样:
+----+-----------+-----------+-----------+
| id | title | full_path | parent_id |
+----+-----------+-----------+-----------+
| 40 | home | 40 | 0 |
| 41 | myhome1 | 41 | 0 |
| 42 | **** | 40-42 | 40 |
| 43 | ***** | 41-43 | 41 |
| 44 | *** | 44 | 0 |
| 45 | **** | 45 | 0 |
| 46 | ***** | 46 | 0 |
| 49 | ****** | 49 | 0 |
| 50 | **** ** | 40-42-50 | 42 |
| 51 | **** ** | 40-42-51 | 42 |
| 52 | **** ** | 40-42-52 | 42 |
| 53 | ******* | 40-53 | 40 |
| 54 | **** | 40-54 | 40 |
| 55 | *** | 41-55 | 41 |
| 56 | **** **** | 40-42-56 | 42 |
| 57 | ******* | 44-57 | 44 |
+----+-----------+-----------+-----------+
我怎样才能得到这样的递归数组:
array
(
40 => array
(
42 => array
(
50,51,52,etc.
),
53,
54
)
41 => array
(
43,
55,
),
44 => array
(
57,
),
etc...
)
我可以使用full_path 创建多级菜单吗?
【问题讨论】:
-
在我看来
full_path是多余的数据,所以我会从数据库中删除它。 -
向我们展示您的尝试,我们可以看到您哪里出错了
-
@Voitcus 没有
full_path如何创建多级菜单? -
要遵循@Voitcus 所说的内容,您的 parent_id 需要是实际的父母,而不是其他祖先。让我们以
#50为例。目前#50的full_path是40-42-50,但在这种情况下,#42应该是#50的父级,#40应该是#42的父级。从#50你会到达#42,从#42你会到达#40- 从那里你会停止循环,因为#40没有父级。 -
你在使用什么 DBMS?
标签: php arrays menu multi-level