Nowadays, more and more level-2 or even multi-level menus are used, and the display and manual designation of foreground menus are increasingly insufficient. Therefore, dynamic Menu Generation is required.
Idea + sample code (take level-2 menu as an example)
First, retrieve the level-1 menu content value. Next, traverse the level-1 menu and use its ID as the parentid of this search. Then, obtain the level-2 menu value corresponding to it,
And add it to the current array (background)
Layer-2 loop. When a value is obtained, check whether there is data for the level-2 menu items. If there is data, the output will be returned. If not, skip (foreground)
Take the PHP background as an Example
$ Res = mysql_query ('*** where parentid = 0'); // obtain the first-level menu content
Foreach ($ res as & $ V)
{
. $ Tres = mysql_query ('*** where parentid ='. $ V ['id']);
$ V ['T'] = $ tres;
}
Front-end
<? PHP if ($ res! = NULL):?>
<? PHP foreach ($ res as $ Val):?>
<Li> outer HTML tag </LI>
<Ul>
<? PHP if ($ res! = NULL):?>
<? PHP foreach ($ res as $ Val):?>
<Li> layer-4 HTML tag </LI>
<? PHP endforeach;?>
<? PHP endif;?>
</Ul>
<? PHP endforeach;?>
<? PHP endif;?>
If you have different opinions, you are welcome to raise them.
Dynamically generate level-2 menus