Php multi-level tree menu code-PHP source code

Source: Internet
Author: User
Tree menus are an algorithm used in many applications. The following section describes the implementation of php tree menus. Tree menus are an algorithm used in many applications. The following section describes the implementation of php tree menus.

Script ec (2); script

Database results


Print as follows:

The Code is as follows:
Array (7 ){
[0] => array (5 ){
["Id"] => string (1) "4"
["Fzname"] => string (12) "bobcat trumpet"
["Userid"] => string (2) "12"
["Pid"] => string (1) "0"
["Time"] => string (10) "1413359977"
}
[1] => array (5 ){
["Id"] => string (1) "5"
["Fzname"] => string (12) "bobcat group"
["Userid"] => string (2) "12"
["Pid"] => string (1) "4"
["Time"] => string (10) "1413361206"
}
[2] => array (5 ){
["Id"] => string (1) "6"
["Fzname"] => string (12) "bobcat group 2"
["Userid"] => string (2) "12"
["Pid"] => string (1) "4"
["Time"] => string (10) "1413361225"
}
[3] => array (5 ){
["Id"] => string (1) "7"
["Fzname"] => string (15) "bobcat group"
["Userid"] => string (2) "12"
["Pid"] => string (1) "5"
["Time"] => string (10) "1413361388"
}
[4] => array (5 ){
["Id"] => string (1) "8"
["Fzname"] => string (15) "bobcat group"
["Userid"] => string (2) "12"
["Pid"] => string (1) "5"
["Time"] => string (10) "1413361409"
}
[5] => array (5 ){
["Id"] => string (1) "9"
["Fzname"] => string (16) "bobcat group 3"
["Userid"] => string (2) "12"
["Pid"] => string (1) "5"
["Time"] => string (10) "1413361460"
}
[6] => array (5 ){
["Id"] => string (2) "10"
["Fzname"] => string (15) "bobcat group 2"
["Userid"] => string (2) "12"
["Pid"] => string (1) "6"
["Time"] => string (10) "1413361506"
}
}

Result:


Print as follows:

The Code is as follows:
Array (1 ){
[0] => array (5 ){
["Id"] => string (1) "4"
["Fzname"] => string (12) "bobcat trumpet"
["Userid"] => string (2) "12"
["Pid"] => array (2 ){
[0] => array (5 ){
["Id"] => string (1) "5"
["Fzname"] => string (12) "bobcat group"
["Userid"] => string (2) "12"
["Pid"] => array (3 ){
[0] => array (5 ){
["Id"] => string (1) "7"
["Fzname"] => string (15) "bobcat group"
["Userid"] => string (2) "12"
["Pid"] => string (0 )""
["Time"] => string (10) "1413361388"
}
[1] => array (5 ){
["Id"] => string (1) "8"
["Fzname"] => string (15) "bobcat group"
["Userid"] => string (2) "12"
["Pid"] => string (0 )""
["Time"] => string (10) "1413361409"
}
[2] => array (5 ){
["Id"] => string (1) "9"
["Fzname"] => string (16) "bobcat group 3"
["Userid"] => string (2) "12"
["Pid"] => string (0 )""
["Time"] => string (10) "1413361460"
}
}
["Time"] => string (10) "1413361206"
}
[1] => array (5 ){
["Id"] => string (1) "6"
["Fzname"] => string (12) "bobcat group 2"
["Userid"] => string (2) "12"
["Pid"] => array (1 ){
[0] => array (5 ){
["Id"] => string (2) "10"
["Fzname"] => string (15) "bobcat group 2"
["Userid"] => string (2) "12"
["Pid"] => string (0 )""
["Time"] => string (10) "1413361506"
}
}
["Time"] => string (10) "1413361225"
}
}
["Time"] => string (10) "1413359977"
}
}

Two Functions 1 are used to generate the Tree Code:

The Code is as follows:
Function getTree ($ data, $ pId)
{
$ Tree = '';
Foreach ($ data as $ k => $ v)
{
If ($ v ['pid '] = $ pid)
{
$ V ['pid '] = $ this-> getTree ($ data, $ v ['id']);
$ Tree [] = $ v;
// Unset ($ data [$ k]);
}
}
Return $ tree;
}

Function 2 is converted to html code:

The Code is as follows:
Function procHtml ($ tree)
{
$ Html = '';
Foreach ($ tree as $ t)
{
If ($ t ['pid '] = '')
{
$ Html. ="
  • {$ T ['fzname']}
  • ";
    }
    Else
    {
    $ Html. ="
  • ". $ T ['fzname'];
    $ Html. = $ this-> procHtml ($ t ['pid ']);
    $ Html = $ html ."
  • ";
    }
    }
    Return $ html? '
      '. $ Html .'
    ': $ Html;
    }

    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.