I need to implement dynamic menu structure using PHP and MySQL. My menu details are in the MySQL table under following columns:
ID | Menu_Name | Position | Menu_Link | Parent_ID
1 | Home | 1 | home.php | 0
2 | Menu1 | 1 | menu1.php| 0
3 | sub1 | 1 | sub1.php | 2
4 | sub2 | 2 | sub2.php | 2
5 | Contact Us | 1 |contact.php| 0
Now...
I have created the table structure in MySQL and fetched all rows using select * from dyn_menu query.
I copied query results into another array using a while loop. hence each row of the table became an element of new array.
i.e. new array is the 'array of arrays'.
The issue is...
In order to display meny items properly, I need to have the menu item exactly in the order mentioned above, however... If I add another menu item in future, it can be present anywhere in the table and it's parent/adjascent menu items might be present before or after the item.
I think...
1. Before displaying this menu structure, I need to sort this using DFS (Depth First Search) algorithm.
but...
I'm not able to sort the 'array of arrays' using DFS algo...
Can anybody suggest a better way? or Does anybody have sample code snippets?