Thinkphp achieves unlimited classification

Source: Internet
Author: User
The above is 1. database Design 2. the data in the added database is parsed as follows: the parent class id of the self-incrementing idpid column in the cid column. The id is 0, indicating the name of the cat_name column in the top-level column p...

The above is

1. database design

2. the added database data is shown in figure

Resolution: cid column auto-increment id

The parent class id of the pid column. if the id is 0, it indicates that it is a top-level column.

Cat_name topic name

Path indicates the hierarchical relationship of a topic.

The implementation results are mainly used:

A mysql function concat (): returns the string generated by the connection parameter.

1. classification display method of the controller

  1. Public function fenlei (){
  2. $ Cate = M ('Category ');
  3. $ List = $ cate-> field ("cid, cat_name, pid, path, concat (path, '-', cid) as bpath")-> order ('bpath ') -> select ();
  4. Foreach ($ list as $ key => $ value ){
  5. $ List [$ key] ['count'] = count (explode ('-', $ value ['bpath']);
  6. }
  7. $ This-> assign ('alist', $ list );
  8. $ This-> display ();
  9. }

2. how to add a controller category

  1. Public function addCat (){
  2. $ Cate = D ('Category ');
  3. If ($ cate-> create ()){
  4. If ($ cate-> add ()){
  5. $ This-> redirect ('/Test/fenlei ');
  6. } Else {
  7. $ This-> error ('add topic failed ');
  8. }
  9. } Else {
  10. $ This-> error ($ cate-> getError ());
  11. }
  12. }

3. topic model

  1. Class CategoryModel extends Model {
  2. Protected $ _ auto = array (
  3. Array ('path', 'tclm', 3, 'callback '),
  4. );
  5. Function tclm (){
  6. $ Pid = isset ($ _ POST ['pid'])? (Int) $ _ POST ['pid']: 0;
  7. // Echo ($ pid );
  8. If ($ pid = 0 ){
  9. $ Data = 0;
  10. } Else {
  11. $ List = $ this-> where ("cid = $ pid")-> find ();
  12. $ Data = $ list ['path']. '-'. $ list ['CID']; // The path of the subclass adds the cid of the parent class to the path of the parent class.
  13. }
  14. Return $ data;
  15. }
  16. }
  17. ?>

4. main html code

  1.  
  2.  
  3.  

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.