PHP generates a classification tree after all categories are removed through the interface, in the case of infinite class classification

Source: Internet
Author: User

Application Scenarios:

Through the web interface to get to the infinite classification of the Department of Data, Department of data parsing out of the format of a two-dimensional array. Because it is the temporary write a simple small frame, did not join the cache mechanism, so can only be the department to display all the page, recursive implementation, but the complexity is too high.


Workaround:

The premise is to use recursive methods, but to generate a new three-dimensional array of data obtained through the interface, and then perform recursive operations, the complexity is relatively low. (If there is a more simple way for the great God, please answer, humbly.) )


Implementation method:

1, processing the data obtained through the interface.

/* * Data processed into a three-dimensional array * @param $dept array json parsed arrays * @return Array * */function Dept_enarray ($dept = Array ()) {    $de Pt_array = Array ();    foreach ($dept as $key = + $val) {        if ($val [' Parentno ']) {            $dept _array[$val [' parentno ']][0][] = $val;        } else{            $dept _array[0][] = $val;        }    }    return $dept _array;}

2. Recursively manipulate the resulting three-dimensional array and generate HTML.

/* * Build Organizational Department tree * @param $orgdept array dept_enarray function processed arrays * @param $html String HTML tag contents * @param $LV int current operation Level * @param $parentarr Array has a department code for the Child department * @param $orgarr array of original incoming arrays * return string HTML * */function Get_depttree ($    orgdept, $html = ', $lv =1, $parentarr = ', $orgarr = ') {$VL = $LV;    if (! $orgarr) $orgarr = $orgdept;    if (! $parentarr && $LV = = 1) $parentarr = Array_keys ($orgdept); foreach ($orgdept [0] as $key = + $val) {if (!in_array ($val [' Deptno '], $parentarr)) $disvisiable = ' class= '        Disvisiable "';        else $disvisiable = '; if ($lv = = 1) {$html. = ' <dl> <dt class= ' f '. $val [' Parentno ']. ' father ' data-deptid= '. $val [ ' Deptno '. ' "status=" close "> <em> </em> <span> '. $val [' name ']. ' </span> <div class= "AddMenu" > <ul> <li class= "Addson" > Add sub-divisions </li> <li class= "AddUser" > Add members </                li> <li> Editing Department </li> <li class= "deldept" > Delete Department </li>        </ul> </div> </dt> '; }else{$html. = ' <dd class= ' list '. ( $VL-1). ' F '. $val [' Parentno ']. ' father ' data-deptid= '. $val [' Deptno ']. ' status= ' close ' > <em><im G src= "/public/images/list-close.png" '. $disvisiable. ' ></em> <span> '. $val [' name ']. ' </span> <div class= "AddMenu" > <ul> <li Class = "Addson" > Add sub-department </li> <li class= "AddUser" > Add Member </li> <                Li> Edit Department </li> <li class= "deldept" > Delete Department </li> </ul>          </div>  </dd> '; } if (In_array ($val [' Deptno '], $parentarr)) {$html = Get_depttree ($orgarr [$val [' deptno]], $html, + + $VL, $pa        Rentarr, $orgarr);    } if ($lv = = 1) $html. = ' </dl> '; } return $html;}

The HTML effect of the last implementation is as follows.



PS: If there is a better solution to the great God, please leave a message, humbly.





PHP generates a classification tree after all categories are removed through the interface, in the case of infinite class classification

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.