Thinkphp achieves unlimited classification (using recursion) _ javascript skills

Source: Internet
Author: User
This article mainly introduces how thinkphp achieves unlimited Classification when recursion is used. Interested friends can refer to the examples in this article to share with you the detailed code of thinkphp to implement unlimited classification, I hope to inspire you to learn about unlimited classification.

Database: test
Data Table :( tp_category ):


Common/conf/config. php

'Db _ config2' => array ('db _ type' => 'mysql', 'db _ user' => 'root ', 'db _ pwd' => '', 'db _ host' => 'localhost', 'db _ port' => '123 ', 'db _ name' => 'test', 'db _ prefix' => 'tp _ ', // database table PREFIX 'db _ charset' => 'utf8 ', // Character Set 'db _ debug' => TRUE, // After the database DEBUG mode is enabled, you can record the SQL log 3.2.3 added ),

Common/function. php traversal function loop

/** Recursive traversal * @ param $ data array * @ param $ id int * return array **/function recursion ($ data, $ id = 0) {$ list = array (); foreach ($ data as $ v) {if ($ v ['pid '] = $ id) {$ v ['son'] = recursion ($ data, $ v ['id']); if (empty ($ v ['son']) {unset ($ v ['son']);} array_push ($ list, $ v) ;}} return $ list ;}

Controller/IndexController. class. php

public function test() { $category = M('category', '', C('DB_CONFIG2'))->select(); $result = loop($category); var_dump($result); $this->assign('list', $result); $this->display();}

Output in the template (View/Index/test.html) (only two levels of classification are supported. If you want to display all types, we recommend that you convert the array to JSON format and then generate it using AJAX requests)

 
 
  • {$vo.category}
    • {$cate.category}

Subsequent (ajax requests show all types recursively ):

Method Controller/IndexController. class. php

Public function test () {$ this-> display ();} public function resultCategory () {$ category = M ('category ','', C ('db _ CONFIG2 ')-> select (); $ result = loop ($ category); $ this-> ajaxReturn (array ('data' => $ result, 'status' => '1', 'info' => 'list retrieved successfully '));}

Template View/Index/test.html

 
  Classification test 
Related Article

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.