在 PHP 中从 5 列 MYSQL 表构建无序列表(树和递归)
我已经尝试解决这个问题有一段时间了,在网上进行研究却一无所获。基本上我有一个代表树结构的 5 列表。您可以将其视为目录列表。我试图从中构建一个无序列表菜单。
这是MYSQL表:
父/子/孙/孙子/孙-孙-孙
France/Aquitaine/Dordogne/Bergerac/Issac
France/Aquitaine/Dordogne/Nontron/Nontron
Cyprus/Paphos/帕福斯区/珊瑚湾/珊瑚湾
丹麦/南丹麦/朗厄兰/鲁德科宾/朗厄兰
埃及/西奈半岛/红海/沙姆沙伊赫/沙姆沙伊赫
无序列表应如下所示:
- 法国
- 阿基坦
- 多尔多涅省
- 贝尔热拉克
- 艾萨克
- 农创
- Nontron
- 贝尔热拉克
- 多尔多涅省
- 阿基坦
- 塞浦路斯
- 帕福斯
- 帕福斯区
- 珊瑚湾
- 珊瑚湾
- 珊瑚湾
- 帕福斯区
- 帕福斯
等...
基本上生成一个代表所有五个级别的无序列表菜单,将所有内容正确分组。
我一直在尝试使用以下功能来尝试得到我想要的东西: 任何
人都可以提供有关如何解决此问题的任何进一步信息吗?我尝试为每个级别构建循环,但很快发现这是徒劳的,应该使用递归函数。
i've been trying to wrap my head around this for a while now, doing research online yielded nothing. Basically i have a 5 column table representing a tree structure. You could think of it as a directory list. Im trying to build a unordered list menu out of this.
This is the MYSQL table:
Parent/Child/Grandchild/Grand-grandchild/grand-grand-grandchild
France/Aquitaine/Dordogne/Bergerac/Issac
France/Aquitaine/Dordogne/Nontron/Nontron
Cyprus/Paphos/Paphos District/Coral Bay/Coral Bay
Denmark/South Denmark/Langeland/Rudkobing/Langeland
Egypt/Sinai Peninsula/Red Sea/Sharm El Sheikh/Sharm El Sheikh
The unordered list should look like this:
- France
- Aquitaine
- Dordogne
- Bergerac
- Isaac
- Nontron
- Nontron
- Bergerac
- Dordogne
- Aquitaine
- Cyprus
- Paphos
- Paphos District
- Coral Bay
- Coral Bay
- Coral Bay
- Paphos District
- Paphos
etc...
Basically producing an unordered list menu representing all five levels, grouping everything correctly.
I have been toying with the following function to try and get what i want:
http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/
could anyone provide any further info on how to solve this? I attempted to build loops for each level but quickly found out this to be futile, and that recursive functions should be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其实这里不需要递归。可以通过重复使用插入函数来构建树,并且可以以命令式方式安全地实现该函数。
一旦你有了这个,如果你的行采用像 array('France','Aquitaine','Dordogne','Bergerac','Issac') 这样的格式,那么在树中插入所有元素就相当容易了。 >:
一种可能的优化是按字典顺序对行进行排序,这意味着具有相似前缀的所有行将被一起处理,从而可以节省一些遍历时间。
Actually, there's no need for recursion here. A tree can be built through repeated usage of an insert function, and that function can be safely implemented in imperative style.
Once you have this, inserting all elements in the tree is fairly easy if your rows are in a format like
array('France','Aquitaine','Dordogne','Bergerac','Issac')
:A possible optimization is to sort the rows lexicographically, which means all rows with a similar prefix will be treated together and thus you can save some traversal time.
好吧,我已经设法拼凑出一段似乎有效的代码。
我首先手动将列连接到一维数组中的 /column1/column2/column3/column4/column5 字符串;
然后应用有问题的 url 中的函数将这个简单数组分解为数组树。
最后,我利用 makeULLI 函数生成一个无序列表,其中包含存在的每个节点的链接。我进一步扩展了代码,根据链接的深度添加自定义路径到 URL,以便在 SEO 友好链接中使用,但我删除了这一点。
我附上执行所有这些操作的代码,并且代码非常小。
该代码应该在任意数量的级别上运行,我已在大约 400 行(5 列)的表上运行它,并且执行时间为 0.0168 秒。
如果有人能看到代码的进一步优化,我将不胜感激。
Ok, i have managed to cobble together a piece of code which seems to work.
I am manually concancenating columns to /column1/column2/column3/column4/column5 strings in a one dimensional array first;
Then applying the functions from url given in question to explode this simple array into an array tree.
Finally i make use of the makeULLI function to generate an unordered list, with links for each node present. I have extended the code further to add custom paths to URLs depending how deep the link is, for use in SEO friendly links but i stripped that bit out.
I am attaching the code which does all this and is suprisingly small.
The code should work on any number of levels, I have run it on a table with around 400 rows (5 columns) and it executes in 0.0168 seconds.
If anyone can see further optimisations to the code i would be grateful.