显示无限的类别结构 -->子类别 --> ETC?
我想这对人们来说是一个相当普遍的要求,当涉及到构建任何类型的依赖于分类和显示数据的应用程序时——任何 CMS/论坛/购物车等,我一直在绞尽脑汁地试图想出一个显示所有类别及其子级的方法无济于事 - 我管理的最好的方法是 while 循环中的 while 循环(无论我认为我可能需要多少级别),但这在我看来违背了编程的要点,它需要毫不费力可扩展的。
因此,给定:
Category 1
-Sub cat
-Sub cat
--Sub sub cat
--- Sub sub cat
-- Sub sub cat
-Sub cat
Category 2
-Sub cat
-Sub cat
--Sub sub cat
---sub sub sub cat
----sub sub sub sub cat
-sub cat
Category 3
-Sub cat
数据库字段:ID 名称 ParentIDS
您将如何从数据库中回显其层次结构中的每个类别?
我会发布我的代码,但显然它很大,考虑到嵌套循环的长期方式。
我考虑过编写一些代码来找到类别树的“深度”,但这仍然没有解决它..想法?
I imagine this is a fairly common requirement for people when it comes to building any kind of application that relies on sorting and displaying data in categories - any CMS/Forums/Carts etc and I've been tearing my hair out trying to think of a way to show all categories and their children to no avail - the best I've managed is while loop within while loop (however many levels I think I may require) but this defeats the point of programming in my opinion, it needs to be effortlessly extensible.
So, given:
Category 1
-Sub cat
-Sub cat
--Sub sub cat
--- Sub sub cat
-- Sub sub cat
-Sub cat
Category 2
-Sub cat
-Sub cat
--Sub sub cat
---sub sub sub cat
----sub sub sub sub cat
-sub cat
Category 3
-Sub cat
Database fields: ID Name ParentIDS
How would you go about echoing each of the categories in their hierarchy from a database?
I'd post my code, but obviously its massive, given the long-hand way of nesting loops.
I've thought about writing bits of code that find the 'depth' of the category tree but that still isn't cutting it.. ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所说的结构本质上是一棵树。如果使用递归,则迭代树相对简单。我不知道您正在使用什么数据库访问代码,但您应该能够从以下伪代码中进行相应推断:
用英语,这意味着打印列表中的一个项目,然后打印其子项,一旦所有子项(和他们的孩子)已打印,请转到列表中的下一项。
The structure you're talking about is essentially a tree. Iterating over a tree is relatively simple if you use recursion. I don't know what database access code you're using, but you should be able to extrapolate accordingly from the following psuedocode:
In english, that means print an item in the list, then print its children, and once all children (and their children) have printed, move on to the next item in the list.
作为提示,您可以使用 Recursion 。一般想法可在 http://en.wikipedia.org/wiki/Recursion 获取。即,您编写了抓取并显示当前文件夹中所有文件和文件夹的函数。然后,您在同一递归循环中对每个子文件夹重复应用相同的函数。等等等等等等。
as a hint, you could use Recursion . General idea is available at http://en.wikipedia.org/wiki/Recursion . I.e. you write function that grabs and displays all files and folders in current folder. Then you repeatedly apply same function for each subfolder in same recursion loop. Etc etc etc.
mysql 网站上有一个非常好的教程。
http://mikehillyer.com/articles/managing-hierarchical-data-in- mysql/
如果您想阅读有趣的部分,请转到
嵌套集模型
坦率地说,我认为没有任何其他/更好的方法来存储层次结构数据。
There is a pretty good tutorial on mysql website.
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
Goto the
The Nested Set Model
if you want to read the interesting partFrankly I do not think there are any other /better way to store hierearchical data.