Foreach group and subgroup dynamic loop
I have this problem. I have a table (below) of groups. It's a recursive sort of table because each new group can have a parent group in the same table. So effectively we have a group > subgroup > subgroup > subgroup kinda model.
**id | label | parent_id**
1 | Ceiling| 0
2 | Window | 0
3 | W开发者_Python百科all | 0
4 | Small | 2
5 | Large | 2
6 | Large| 1
7 | Paint | 4
So this would give something that looks like this:
Window > Small window > Paint
I've created the forms and table for creating the groups but it's the database query and loops that I'm having trouble with actually getting data into the above format. Bit too much for my brain to handle :(
I'm doing it in this format because I want there to be complete control over the groups and the depth of the subgroups.
I don't really have code to give an example because it's more the problem solving I'm after.
** UPDATE **
A bit more specific: I want to list each parent group (so a group that has a 0 set in parent_id) and it's immediate subgroup, then that groups immediate subgroup (if it has any) etc, etc.
If you want to do it like this, you will allways have to fetch whole table to PHP and then perform the search with php.
However, there is one similar method how you can managet such structure, it is very vell described here: http://www.sitepoint.com/hierarchical-data-database-2/
精彩评论