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; } |