How to implement Infinitus classification in PHP? Three implementation methods of Infinitus classification (detailed code explanation)

青灯夜游
Release: 2023-04-04 11:16:01
forward
5887 people have browsed it

The content of this article is to introduce how to implement Infinitus classification in PHP? There are three implementation methods of Infinitus classification (detailed code explanation). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Infinitus Category 1:

public function judeg($id)
{
   $rs = Db::name('finance_class') -> field('parent_code') -> where('id',$id) -> select();
   $i = 1;
   foreach($rs as $k => $v){
     if($v['parent_code'] <> 0){
       $i += $this -> judeg($v['parent_code']);
      }
   }
   return $i;
}

 public function cid($id,$pid)
 {
    $w['parent_code'] = $id;
    $rs = Db::name('finance_class')
       -> field('id,code,name,parent_code')
       -> where($w)
       -> order('code asc')
       -> select();
    $str = '';
    foreach ($rs as $k => $v) {
        $name = $v['name'];
        $_id = $v['id'];
        $cutOff = '';
        for($i = 0; $i < $this -> judeg($_id); $i++){
          $cutOff.='-';
        }
        if($_id == $pid){
          $str.='<p>他的id='.$_id.'====他的级别'.$cutOff.$name.'</p>';
        }else{
          $str.='<p>他的id='.$_id.'====他的级别'.$cutOff.$name.'</p>';
        }
        $str.=$this->cid($_id,$pid);
    }
    return $str;
37}
public function finance_c()
{
   $w['type'] = '资产类';
   $w['parent_code'] = 0;
   $rs = Db::name('finance_class')
       -> field('id,code,name,parent_code')
       -> where($w)
        -> select();
   $str = '';
   foreach ($rs as $k => $v){
     $str.= '<p>一级name:'.$v['name'].'</p>';
     $str.=    $this -> cid($v['id'],0);
   }
   echo $str;
}
Copy after login

The efficiency of this method is slow and convoluted.

Method 2:

public function getVoucherClass()
{
    $lists = Db::name(&#39;finance_class&#39;)->select();

    $lists = $this->getTree($lists);

    foreach($lists as $value){
      echo str_repeat(&#39;--&#39;, $value[&#39;level&#39;]), $value[&#39;name&#39;].&#39;<br />&#39;;
    }
10}

/**
* 递归实现无限极分类
* @param $array 分类数据
* @param $pid 父ID
* @param $level
* @return $list 
*/
function getTree($array, $pid =0, $level = 0){
   static $list = [];
   foreach ($array as $key => $value){
     if ($value[&#39;parent_code&#39;] == $pid){
        $value[&#39;level&#39;] = $level;
         $list[] = $value;
         unset($array[$key]);
         $this->getTree($array, $value[&#39;id&#39;],$level+1);
      }
   }
   return $list;
30}
Copy after login

Infinitus Classification 3:

 public function index()
    {
       $lists = \think\Db::table(&#39;ozyx_finance_class&#39;)->select();
       $lists = $this->getTree($lists,0);

       foreach ($lists as $k => $v) {
               $lists_one[$v[&#39;type&#39;]][]=$v;
       }

       // halt($lists_one);
       $this->assign(&#39;lists&#39;, $lists_one);

       return view();
    }

    /**
    * 无限极分类
    */
    function getTree($data, $pid)
    {
        $tree = &#39;&#39;;
        foreach($data as $k => $v)
        {
          if($v[&#39;parent_code&#39;] == $pid)
          {        
               $v[&#39;parent_code&#39;] = $this->getTree($data, $v[&#39;id&#39;]);
               $tree[] = $v;
                  unset($data[$k]);
          }
        }
        return $tree;
    }
Copy after login

Data table structure:

The above is the detailed content of How to implement Infinitus classification in PHP? Three implementation methods of Infinitus classification (detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template