How does PHP implement the tree-like effect? (Attached code)

Source: Internet
Author: User
Tags php foreach
What this article brings to you is about how PHP implements the tree-like effect of classification. (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.

We sometimes need to display the categorical data in a tree-like effect, and at a glance we can see the hierarchy of organizational relationships, which are implemented as follows:

Note: I am talking about the classification for the Infinite Pole classification, as the following function refers to the table field name has three ID PID name, if there is a definition of different names please query the categorical list data when corresponding to the field alias corresponding to the ID PID name, such as your PID table field name defined as parent_id so query When the field is named "parent_id as PID";

The functions are as follows:

/** * $list  for the query out of the two-dimensional array **/function gettree ($list, $pid =0, $itemprefix = ") {Static $icon = Array (' │ ', ' ├ ', ' └ ');    Static $nbsp = " "; Static $arr = Array ();    $number = 1;    foreach ($list as $row) {    if ($row [' pid '] = = $pid) {    $brotherCount = 0;    Determine how many siblings currently have a    foreach ($list as $r) {    if ($row [' pid '] = = $r [' pid ']) {    $brotherCount + +;    }    }    if ($brotherCount >0) {    $j = $k = ';    if ($number = = $brotherCount) {    $j. = $icon [2];                    $k = $itemprefix? $NBSP: ';    } else{                    $j. = $icon [1];                    $k = $itemprefix? $icon [0]: ';    }    $spacer = $itemprefix? $itemprefix. $j: ";    $row [' name '] = $spacer. $row [' name '];    $arr [] = $row;    $number + +;    Gettree ($list, $row [' id '], $itemprefix. $k. $nbsp);    }}} return  $arr;}

Examples of Use:

<?php header ("content-type:text/html;    Charset=utf8 "), function Gettree ($list, $pid =0, $itemprefix =") {Static $icon = Array (' │ ', ' ├ ', ' └ '); Static $nbsp = "&nbsp;";    Static $arr = Array ();    $number = 1;    foreach ($list as $row) {if ($row [' pid '] = = $pid) {$brotherCount = 0;    Determine how many siblings currently have a foreach ($list as $r) {if ($row [' pid '] = = $r [' pid ']) {$brotherCount + +;    }} if ($brotherCount >0) {$j = $k = ';                    if ($number = = $brotherCount) {$j. = $icon [2]; $k = $itemprefix?    $NBSP: ";                    }else{$j. = $icon [1]; $k = $itemprefix?    $icon [0]: "; } $spacer = $itemprefix? $itemprefix.    $j: ";    $row [' name '] = $spacer. $row [' name '];    $arr [] = $row;    $number + +;    Gettree ($list, $row [' id '], $itemprefix. $k. $nbsp); }}} return $arr;} $list the data from the mock database $list = Array (), $list = [' id ' =>1, ' pid ' =>0, ' name ' = '],[' id ' =>9, ' pid ' =>7, ' Name ' + ' personnel two Group Staff 2 '],[' ID ' =&GT;2, ' pid ' =>1, ' name ' = ' hr manager '],[' id ' =>3, ' pid ' =>2, ' name ' = ' Personnel one leader '],[' id ' =>13, ' pid ' =>12, ' Name ' = = ' technology A group of staff 1 '],[' id ' =>4, ' pid ' =>3, ' name ' + ' personnel a group of staff 1 '],[' id ' =>5, ' pid ' =>3, ' name ' = ' Personnel a group of staff 2 '],[' id ' =>6, ' pid ' =>3, ' name ' and ' + ' personnel a group of staff 3 '],[' id ' =>7, ' pid ' =>2, ' name ' = ' Personnel two leader '],[' ID ' 8, ' pid ' =>7, ' name ' + ' personnel two Group staff 1 '],[' id ' =>10, ' pid ' =>7, ' name ' = ' Personnel two Group Staff 3 '],[' id ' =>15, ' pid ' =>12, ' Name ' = = ' technology A group of staff 3 '],[' id ' =>11, ' pid ' =>1, ' name ' = ' technology manager '],[' id ' =>12, ' pid ' =>11, ' name ' = ' Technology a leader ' ],[' id ' =>14, ' pid ' =>12, ' name ' = ' Technology A group of Staff 2 '],];//execute function $list = Gettree ($list);? > <! DOCTYPE html>

Effect:

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.