I am planning to create a multi select drop down or multi select check box form element in php. But with a parent child menu item relation ship.
Ex: So I have got three separate tables tbl_branch, tbl_area, tbl_region and 2 more tables for the mapping tbl_regiontoarea and tbl_areatobranch. Now I need a multi select or multi check box which looks some thing like this:
Branch 1
Area 1
Region 1
Region 2
Area 2
Region 3
Region 4
Region 7
Region 9
Area 5
Area 7
Region 8
Branch 2
Area 3
Branch 3
Branch 4
Branch 5
Area 6
Region 10
Area 2
So as you would have guessed it, Branch is the top most in the hierarchy and region is the lowermost i.e. Branch is the parent. I would like it to loop until it finds children for it. Once it stops finding any children it would move on to next parent item in the array.
I am very bad with arrays, and forming complex sql queries and hence was looking forward to have some starting direction on how to do this. I would really appreciate if some one could help me with the same.
Thanks in advance
I suggest you give this a read
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
I’ve set up a tree of near identical data and using the methods suggested in the above document make the issue significantly simpler especially when it comes to determining parents and siblings down to n levels.
The short version is that you assign a number on ‘left’ and ‘right’ of each node, and using that you can then select segments of the tree very efficiently. The more long winded version is in that article, and explains it far more thoroughly than I can and includes useful MySQL code.
Here’s a potential outline for your main table
If you need specific data for each type then you could create seperate tables for them but the above would let you create your tree, map out the id’s and allow relationships to be stored along with a display name.
Of course the more data you can normalise between each leafType the better, ideally they are all effectively the same type of data after all as they are all a geographic subsection
Taking a sample of your data as example
you might end up with table such as
Queries for how to insert etc can be found in the linked article, i.e. (quoted verbatim from http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/)
And then a SELECT can illustrate the tree (again verbatim from the source)
Field/table names in the 2 above are of course unchanged, but you can update the table/field names as necessary
Following my latest comment, below is the definition for my stored procedure I use for inserting new rows
This is then called using calls such as the following