Well, to build my menu my menu I use a db similar structure like this
2 Services 0 3 Photo Gallery 0 4 Home 0 5 Feedback 0 6 FAQs 0 7 News & Events 0 8 Testimonials 0 81 FACN 0 83 Organisation Structure 81 84 Constitution 81 85 Council 81 86 IFAWPCA 81 87 Services 81 88 Publications 81
To assign another submenu for existing submenu I simply assign its parent's id as its value of parent field. parent 0 means top menu
now there is not problem while creating submenu inside another submenu
now this is way I fetch the submenu for the top menu
<ul class="topmenu">
<? $list = $obj -> childmenu($parentid);
//this list contains the array of submenu under $parendid
foreach($list as $menu) {
extract($menu);
echo '<li><a href="#">'.$name.'</a></li>';
}
?>
</ul>
What I want to do is.
I want to check if a new menu has other child menu
and I want to keep on checking until it searches every child menu that is available
and I want to display its child menu inside its particular list item like this
<ul>
<li><a href="#">Home</a>
<ul class="submenu">
........ <!-- Its sub menu -->
</ul>
</li>
</ul>
With a database structure like yours, it is possible to build the whole HTML menu with a single query and without recursion.
Yes - I will repeat:
This is the approach I always use myself.
Pasted the code here - fully functional:
http://pastebin.com/GAFvSew4
Jump to line 67 to see the interesting part ("get_menu_html").
The main loop starts at line 85.
There are five "customizable" HTML snippets:
(The code could be cleaner if I hadn't worried with tabulation.)
SQL to create and populate sample database is available at the end of the script.
You can try and let us know your thoughts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With