简体   繁体   English

“自引用”表和数组

[英]“self-referencing” table and arrays

MySQL table: MySQL表:

categoryID  
categoryName  
categoryParent

Every category has ONE parent category, though it can be NULL, which I treat as the root-category. 每个类别都有一个父类别,尽管它可以为NULL,我将其视为根类别。

I want to get all categories from the table, store it in an array and print it in a way, that shows the nesting. 我想从表中获取所有类别,将其存储在数组中并以显示嵌套的方式进行打印。

Example: 例:

ID  name    parent  
1   a       NULL  
2   b       NULL  
3   c       NULL  
4   b1      2  
5   d       NULL  
6   b2      2 

HTML: HTML:

a  
b  
-b1  
-b2  
c  
d  

Later I'll try to make it draggable with jQuery so the user can choose the parent/child category by him-/herself. 稍后,我将尝试使其与jQuery一起可拖动,以便用户可以自己选择父/子类别。

Can I do all this with one single table or do I need an external junction table? 我可以只用一张桌子完成所有这些工作,还是需要一个外部联结表?

Your table structure is fine. 您的表结构很好。

You'll render the nesting when you deal with the results; 处理结果时,将渲染嵌套。 perhaps you loop through each result with no parent and append to the DOM, then loop through each result with a parent appending to existing elements. 也许您在没有父项的情况下循环访问每个结果,并将其附加到DOM,然后在父项附加到现有元素后遍历每个结果。 That only works for a two-level tree, but you get the idea. 这仅适用于两级树,但是您知道了。

Yes you can, just select all the rows from table. 是的,您可以,只需从表中选择所有行。 And using php, you can generate a good html table using a for loop. 使用php,您可以使用for循环生成一个很好的html表。 When you see an element with a parent, just process the table string you are constructing in your php script. 当您看到带有父元素的元素时,只需处理您在php脚本中构造的表字符串。 From there using JQuery you can change the orders and parent child relationship of the table. 从那里可以使用JQuery更改表的顺序和父子关系。

You can nest arrays and have a structure like (Array of (me, childrenArray)). 您可以嵌套数组,并具有类似((me,childrenArray)的数组)的结构。 That way you use the "me" for indexing. 这样,您可以使用“我”进行索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM