PHP unlimited classification best practices

Source: Internet
Author: User
Infinitus classification is a common and essential function, which is available in almost every project. Application scenarios: pull-down lists, tree lists, and other infinitely classified front-end implementations (the front-end framework has generally been implemented, as long as the backend transmits data to the front-end in the specified format) backend implementation... unlimited classification
  • It is a common and necessary function, which is available in almost every project.

  • Application scenarios: drop-down lists, tree lists, etc.

Unlimited classification type
  • Front-end implementation(The front-end framework is generally implemented, as long as the backend transmits data to the front-end in the specified format)

  • Backend implementation(This implementation is mainly described below)

Unlimited implementation
  • First (recommended)

function infiniteSort($data, $showFName, $titleFName, $pidFName = 'pid', $idFName = 'id', $levelFName = 'level', $pid = 0, $level = 0){    $tree = array();    foreach ($data as $key => $value) {        if ($value[$pidFName] == $pid) {            $value[$levelFName] = $level;            $value[$showFName] = str_repeat('  ', $level) . '|-' . $value[$titleFName];            $tree[] = $value;            unset($data[$key]);            $tempArr = infiniteSort($data, $showFName, $titleFName, $pidFName, $idFName, $level, $value[$idFName], $level + 1);            if(!empty($tempArr)){                $tree = array_merge($tree, $tempArr);            }        }    }    return $tree;}

Note:
1. $ dataAll data sorted by asc
2. $ showFNameDisplay the field name of the name (formatted)
3. $ titleFNameField name of the title (not formatted)
4. $ levelFNameLevel field name
5. $ pidFNameField name of the parent id
6. $ idFNameId field name

  • Second (using reference variables)

/*** Unlimited classification ** @ param Array $ treeList // an Array that receives processed data * @ param Array $ data // The result set obtained in the database * @ param String $ level // format the hierarchical field name * @ param Int $ pid * @ param Int $ count // level of classification */function tree (& $ treeList, & $ data, $ level, $ show_name, $ field_name, $ field_pid = 'pid', $ field_id = 'id', $ pid = 0, $ count = 0) {foreach ($ data as $ key => $ value) {if ($ value [$ field_pid] ==$ pid) {$ value [$ level] = $ count; $ value [$ show_name] = str_repeat ('', $ count ). '| -'. $ value [$ field_name]; $ treeList [] = $ value; unset ($ data [$ key]); tree ($ treeList, $ data, $ level, $ show_name, $ field_name, $ field_pid, $ field_id, $ value [$ field_id], $ count + 1 );}}}

Note:
1. $ dataAll data sorted by asc
2. the returned list of Infinity data is included in $ treeList.

  • Third(There is a limit on the use of static variables: if one request is called twice to implement two unlimited classes, the problem will occur, so it is not recommended)

Public function getTree ($ list, $ parent_id, $ level = 0) {// it should be a static local variable, so as to ensure that, in recursive calls, all // getTree methods operate on a Tree space. Static $ tree = array (); // Save the array of the found classes. // traverse all the classes and use parent_id to determine which ones are the foreach ($ list as $ row) we are looking) {// Determine the currently traversed Category $ row and whether it is the subcategory to be searched. if ($ row ['pid '] = $ parent_id) {// locate a category. // save it. where? $ Row ['level'] = $ level; $ tree [] = $ row; // continue searching for the subcategory of the current $ row category $ this-> getTree ($ list, $ row ['id'], $ level + 1 );}} return $ tree ;}

Note:
1. $ listAll data sorted by asc

Unlimited classification
  • It is a common and necessary function, which is available in almost every project.

  • Application scenarios: drop-down lists, tree lists, etc.

Unlimited classification type
  • Front-end implementation(The front-end framework is generally implemented, as long as the backend transmits data to the front-end in the specified format)

  • Backend implementation(This implementation is mainly described below)

Unlimited implementation
  • First (recommended)

function infiniteSort($data, $showFName, $titleFName, $pidFName = 'pid', $idFName = 'id', $levelFName = 'level', $pid = 0, $level = 0){    $tree = array();    foreach ($data as $key => $value) {        if ($value[$pidFName] == $pid) {            $value[$levelFName] = $level;            $value[$showFName] = str_repeat('  ', $level) . '|-' . $value[$titleFName];            $tree[] = $value;            unset($data[$key]);            $tempArr = infiniteSort($data, $showFName, $titleFName, $pidFName, $idFName, $level, $value[$idFName], $level + 1);            if(!empty($tempArr)){                $tree = array_merge($tree, $tempArr);            }        }    }    return $tree;}

Note:
1. $ dataAll data sorted by asc
2. $ showFNameDisplay the field name of the name (formatted)
3. $ titleFNameField name of the title (not formatted)
4. $ levelFNameLevel field name
5. $ pidFNameField name of the parent id
6. $ idFNameId field name

  • Second (using reference variables)

/*** Unlimited classification ** @ param Array $ treeList // an Array that receives processed data * @ param Array $ data // The result set obtained in the database * @ param String $ level // format the hierarchical field name * @ param Int $ pid * @ param Int $ count // level of classification */function tree (& $ treeList, & $ data, $ level, $ show_name, $ field_name, $ field_pid = 'pid', $ field_id = 'id', $ pid = 0, $ count = 0) {foreach ($ data as $ key => $ value) {if ($ value [$ field_pid] ==$ pid) {$ value [$ level] = $ count; $ value [$ show_name] = str_repeat ('', $ count ). '| -'. $ value [$ field_name]; $ treeList [] = $ value; unset ($ data [$ key]); tree ($ treeList, $ data, $ level, $ show_name, $ field_name, $ field_pid, $ field_id, $ value [$ field_id], $ count + 1 );}}}

Note:
1. $ dataAll data sorted by asc
2. the returned list of Infinity data is included in $ treeList.

  • Third(There is a limit on the use of static variables: if one request is called twice to implement two unlimited classes, the problem will occur, so it is not recommended)

Public function getTree ($ list, $ parent_id, $ level = 0) {// it should be a static local variable, so as to ensure that, in recursive calls, all // getTree methods operate on a Tree space. Static $ tree = array (); // Save the array of the found classes. // traverse all the classes and use parent_id to determine which ones are the foreach ($ list as $ row) we are looking) {// Determine the currently traversed Category $ row and whether it is the subcategory to be searched. if ($ row ['pid '] = $ parent_id) {// locate a category. // save it. where? $ Row ['level'] = $ level; $ tree [] = $ row; // continue searching for the subcategory of the current $ row category $ this-> getTree ($ list, $ row ['id'], $ level + 1 );}} return $ tree ;}

Note:
1. $ listAll data sorted by asc

For more articles about PHP unlimited classification best practices, refer to PHP Chinese network!

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.