PHP recursive implementation hierarchy tree expansion, php recursive hierarchy tree _ PHP Tutorial

Source: Internet
Author: User
Tags learn php programming
PHP recursion implements hierarchical tree expansion, and php recursion hierarchy tree. PHP recursive implementation hierarchical tree expansion, php recursive hierarchy tree this article examples for you to share the main code of PHP recursive implementation hierarchical tree expansion, for your reference, the specific content is as follows: PHP recursively implements hierarchical tree expansion, and php recursion hierarchy tree

The examples in this article share the main code for the hierarchical tree expansion of PHP recursive implementation for your reference. the specific content is as follows:

:

Implementation code:

<? Php $ db = mysql_connect ('localhost', 'root', 'root') or die ('Can \'t connect to database'); mysql_select_db ('test ') or die ('Can \'t find database: test'); $ result = mysql_query ('select id, fid, name from tree '); while ($ arr = mysql_fetch_array ($ result) {$ data [] = array ('id' => $ arr ['id'], 'fid' => $ arr ['fid'], 'name' => $ arr ['name'],);} // sort data by indentation. for details, see function data2arr ($ tree, $ rootId = 0, $ level = 0) {foreach ($ tree as $ leaf) {if ($ leaf ['fid'] = $ rootId) {echo str_repeat ('', $ level ). $ leaf ['id']. ''. $ leaf ['name'].'
'; Foreach ($ tree as $ l) {if ($ l ['fid'] = $ leaf ['id']) {data2arr ($ tree, $ leaf ['id'], $ level + 1); break ;}}} data2arr ($ data); echo'
-----------------------------------------------------------------------
'; // Encapsulate data according to the link. for details, see function arr2tree ($ tree, $ rootId = 0) {$ return = array (); foreach ($ tree as $ leaf) {if ($ leaf ['fid'] = $ rootId) {foreach ($ tree as $ subleaf) {if ($ subleaf ['fid'] = $ leaf ['id']) {$ leaf ['Children '] = arr2tree ($ tree, $ leaf ['id']); break ;}$ return [] = $ leaf ;}return $ return ;}$ tree = arr2tree ($ data ); print_r ($ tree); echo'
-----------------------------------------------------------------------
'; // Use HTML to display data again. for details, see function tree2html ($ tree) {echo'
    '; Foreach ($ tree as $ leaf) {echo'
  • '. $ Leaf ['name']; if (! Emptyempty ($ leaf ['Children ']) tree2html ($ leaf ['Children']); echo'
  • ';} Echo'
';} Tree2html ($ tree );

The above is all the content of this article. I hope it will help you learn php programming.

Articles you may be interested in:
  • Analysis on the use of PHP recursive function return values
  • Php unlimited classification and support for output tree chart details
  • Php recursively retrieves files in the Directory (including subdirectories) and shares encapsulation classes
  • Considerations for using return in php recursive functions
  • Example of php recursion (php recursion function)
  • How to use php function recursion and difference between return and echo
  • Php unlimited classification, super simple unlimited classification, support output tree chart
  • Php Infinitus classification recursive sorting implementation method
  • Php tree menu code based on recursion
  • Three basic methods for php to implement recursion

Examples in this article share the main code of hierarchical tree expansion for PHP recursion implementation for your reference. the specific content is as follows...

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.