The method of thinkphp automatic filling to realize infinite level classification _php example

Source: Internet
Author: User

This article shows the method of thinkphp automatic filling to realize infinite level classification, which is one of the common functions of thinkphp, and has practical value. The complete example is now shared with you for your reference. The following steps are implemented:

The table aoli_cate as shown in the following illustration:

One, the action part:

aoli/home/lib/action/cataaction.class.php files are as follows:

<?php
class Cateaction extends action{
  function index () {
    $cate =m (' Cate ');
    $list = $cate->field ("Id,name,pid,path,concat (Path, '-', id) as Bpath")->order (' Bpath ')->select ();
    foreach ($list as $key => $value) {
       $list [$key] [' Count ']=count (Explode ('-', $value [' Bpath ']);
    }
    $this->assign (' alist ', $list);
    $this->display ();  
  }
  Add column
  function add () {
    $cate =new catemodel ();
    
    if ($vo = $cate->create ()) {
      if ($cate->add ()) {
        $this->success (' Add column succeeded ');  
      } else{
        $this->error (' Add column failed ');  
      }
      Dump ($VO);  
    else{
      $this->error ($cate->geterror ());}}
? >

Second, the model part:

aoli/home/lib/model/catamodel.class.php files are as follows:

<?php
class Catemodel extends model{//table aoli_cate protected $_auto=array
  (
    Array (' path ', ' TCLM ') in the corresponding database , 3, ' callback ')  
  ; 
  
  function Tclm () {
    $pid =isset ($_post[' pid '))? ( int) $_post[' pid ']:0;
    Echo ($pid);
    if ($pid ==0) {
      $data =0;
    } else{
      $list = $this->where ("id= $pid")->find ();
      $data = $list [' Path ']. -'. $list [' ID '];//Subclass's path is the parent class's path plus the parent class's ID
    } return
    $data;  
  }
? >

Third, view views section

The aoli/home/tpl/default/cate/index.html page is as follows:

<form action= "__url__/add" method= "POST" >
 Please select parent column: <select name= "pid" size= ">
         <option" Value= "0" > Root column </option>
         <volist name= "alist" id= "Vo" >
          <option value= "{$vo [' id ']}" >
            <php> for
              ($i =0 $i < $vo [' count ']; $i + +) {
                echo ';  
              }
            </php>
            {$vo [' name ']}
          </option>
         </volist>
        </select><br/>
 new column name: <input type= "text" name= "name"/><br/> <input type=
 "Submit" value= "add column"/>
</form>

It is believed that the examples mentioned in this paper have some reference value for thinkphp programming.

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.