I have created a MySQL database table where I want to show the menu structure based on their parent. The menu table contains the following data:
id | menuname | parentid
---+-------------------+---------
1 | dashboard | 0
2 | Content | 0
3 | Home Page Content | 2
4 | Banners | 2
5 | Settings | 0
6 | Block Content | 3
7 | Site Content | 3
So that the menu structure will look like:
- dashboard
- Content
- Home Page Content
- Block Content
- Site Content
- Home Page Content
- Banners
- Settings
I have gone though the post over here Recursive menu tree from array however, unable to understand the basics. However, the idea I understood that I need to write a recursive function to sort out this. Can someone give me some ideas how to generate the above?
select * from my_table order by parentid ASC, id ASCand the rest will need to be PHP logic. Also, why isn'tBannersa child ofContentin your example? If you are interested in hierarchical table structures and queries in MySQL then you can check out stackoverflow.com/q/20215744/2191572