PHP Infinite class classification best practices

Source: Internet
Author: User

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

    • The first kind (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;} 

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

    • Second (using reference variables)

/** * 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

    • Third (there is a limit to the use of static variables: If a request is called two times to achieve 2 infinite class classification will be problematic, so not recommended)

    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

    • The first kind (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;}

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

    • Second (using reference variables)

/** * 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

    • Third (there is a limit to the use of static variables: If a request is called two times to achieve 2 infinite class classification will be problematic, so not recommended)

    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

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.