List_to_tree of Thinkphp allows you to list all nodes in an infinite number of categories. thinkphptree

Source: Internet
Author: User

List_to_tree of Thinkphp allows you to list all nodes in an infinite number of categories. thinkphptree
List_to_tree is very convenient to use. For more information, see the manual. Because I need to list all nodes at the same time when using it, I wrote a recursive function for your reference.

Public function index () {Load ('extend'); // Load the extension method $ Category = D ('category '); $ list = $ Category-> order ('sort desc')-> select (); // sort nodes of the same level. $ list = list_to_tree ($ list, 'id ', 'fid'); // For detailed parameters, see $ list = $ this-> findChild ($ list); dump ($ list);} protected function findChild ($ arr) {static $ tree = array (); foreach ($ arr as $ key => $ val) {$ tree [] = $ val; if (isset ($ val ['_ child']) {$ this-> findChild ($ val ['_ child']) ;}} return $ tree ;}


 

/*** Convert the returned dataset to Tree * @ access public * @ param array $ list the dataset to be converted * @ param string $ pid parent tag field * @ param string $ level mark the field * @ return array */function list_to_tree ($ list, $ pk = 'id', $ pid = 'pid', $ child = '_ child', $ root = 0) {// create Tree $ tree = array (); if (is_array ($ list) {// create a primary key-based array reference $ refer = array (); foreach ($ list as $ key => $ data) {$ refer [$ data [$ pk] = & $ list [$ key];} foreach ($ list as $ key => $ data) {// determine whether parent $ parentId = $ data [$ pid] exists; if ($ root = $ parentId) {$ tree [] = & $ list [$ key];} else {if (isset ($ refer [$ parentId]) {$ parent = & $ refer [$ parentId]; $ parent [$ child] [] = & $ list [$ key] ;}}} return $ tree ;} /*** sort the query result set * @ access public * @ param array $ list query result * @ param string $ field name * @ param array $ sortby sorting type * asc forward sorting desc reverse sorting nat natural sorting * @ return array */function list_sort_by ($ list, $ field, $ sortby = 'asc ') {if (is_array ($ list) {$ refer = $ resultSet = array (); foreach ($ list as $ I => $ data) $ refer [$ I] = & $ data [$ field]; switch ($ sortby) {case 'asc ': // asort ($ refer); break; case 'desc': // arsort ($ refer); break; case 'nat ': // natural sorting natcasesort ($ refer); break;} foreach ($ refer as $ key => $ val) $ resultSet [] = & $ list [$ key]; return $ resultSet;} return false ;} /*** search for * @ access public * @ param array $ list data list * @ param mixed $ condition query condition * in the data list. array ('name' => $ value) or name = $ value * @ return array */function list_search ($ list, $ condition) {if (is_string ($ condition) parse_str ($ condition, $ condition ); // returned result set $ resultSet = array (); foreach ($ list as $ key => $ data) {$ find = false; foreach ($ condition as $ field =>$ value) {if (isset ($ data [$ field]) {if (0 === strpos ($ value, '/') {$ find = preg_match ($ value, $ data [$ field]);} elseif ($ data [$ field] = $ value) {$ find = true ;}}if ($ find) $ resultSet [] = & $ list [$ key] ;}return $ resultSet ;}




Php + mysql three-level classification, Li Wenkai thinkphp unlimited Classification

So, what do you want to ask? Well, I have watched this video,
 
Who can send me a thinkphp project background with systematic settings, seo optimization, adding modules, templates, and other functions? Unlimited column classification?

You just need to go to the next phpcms. thinkphp is used for development, but the structure is changed. There are also manuals on the Internet, which are easy to understand.

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.