Dynamically generate level-2 menus

Source: Internet
Author: User
Tags php foreach

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.