Infinite class classification
is a very common and very necessary feature that almost every project has.
Scenario: drop-down list, tree list, etc.
Types of infinite class classifications
Front -end implementation (front-end framework is generally implemented, as long as the backend in the specified format to transmit data to the front-end can be generated)
back-end implementations (this is mainly the implementation below)
Infinite levels of multiple implementations
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;}
Attention:
1. $data All data that has been ASC sorted
2, $showFName display the name of the field name (formatted)
3. field name of $titleFName title (unformatted)
4. $levelFName Level field name
5, $pidFName the name of the parent ID field
6. field name of $idFName ID
/** * Unlimited class * @param array $treeList//arrays that accept processing complete data * @param The result set obtained in array $data//database * @param String $level//format Level field name * @ param int $pid * @param int $count//Sub-category */function Tree (& $treeList, & $data, $level, $show _name, $field _name, $f ield_pid = ' pid ', $field _id = ' id ', $pid = 0, $count = 0) { foreach ($data as $key + = $value) { if ($value [$fiel D_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);}}
Attention:
1. $data All data that has been ASC sorted
2, the return of the infinite level list of data exist $treelist inside
Public Function Gettree ($list, $parent _id, $level =0) { ///should be a static local variable so that all //Gettree methods are guaranteed at recursive invocation, The operation is a tree space. Static $tree = Array ();//save an array of found classifications //traverse all classifications, judging by parent_id, which is the foreach we are looking for ($list as $row) { // Determines whether the currently traversed classification $row is the sub-category if ($row [' pid '] = = $parent _id) { ///found a category //Saved up, where is it stored? $row [' level '] = $level; $tree [] = $row; Continue to find subcategories of the classifications represented by the current $row $this->gettree ($list, $row [' id '], $level + 1);} } return $tree; }
Attention:
1. $list All data that has been ASC sorted
Infinite class classification
is a very common and very necessary feature that almost every project has.
Scenario: drop-down list, tree list, etc.
Types of infinite class classifications
Front -end implementation (front-end framework is generally implemented, as long as the backend in the specified format to transmit data to the front-end can be generated)
back-end implementations (this is mainly the implementation below)
Infinite levels of multiple implementations
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;}
Attention:
1. $data All data that has been ASC sorted
2, $showFName display the name of the field name (formatted)
3. field name of $titleFName title (unformatted)
4. $levelFName Level field name
5, $pidFName the name of the parent ID field
6. field name of $idFName ID
/** * Unlimited class * @param array $treeList//arrays that accept processing complete data * @param The result set obtained in array $data//database * @param String $level//format Level field name * @ param int $pid * @param int $count//Sub-category */function Tree (& $treeList, & $data, $level, $show _name, $field _name, $f ield_pid = ' pid ', $field _id = ' id ', $pid = 0, $count = 0) { foreach ($data as $key + = $value) { if ($value [$fiel D_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);}}
Attention:
1. $data All data that has been ASC sorted
2, the return of the infinite level list of data exist $treelist inside
Public Function Gettree ($list, $parent _id, $level =0) { ///should be a static local variable so that all //Gettree methods are guaranteed at recursive invocation, The operation is a tree space. Static $tree = Array ();//save an array of found classifications //traverse all classifications, judging by parent_id, which is the foreach we are looking for ($list as $row) { // Determines whether the currently traversed classification $row is the sub-category if ($row [' pid '] = = $parent _id) { ///found a category //Saved up, where is it stored? $row [' level '] = $level; $tree [] = $row; Continue to find subcategories of the classifications represented by the current $row $this->gettree ($list, $row [' id '], $level + 1);} } return $tree; }
Attention:
1. $list All data that has been ASC sorted