关于ThinkPhp5 无限极分类的记录

数据库

建表

DROP TABLE IF EXISTS `yr_cate`;
CREATE TABLE `yr_cate` (
  `cateid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '栏目id',
  `catename` varchar(50) NOT NULL COMMENT '栏目名称',
  `pid` int(10) NOT NULL,
  `path` varchar(200) NOT NULL COMMENT '路径',
  `level` int(10) NOT NULL COMMENT '级别',
  `model` enum('5','4','3','2','1') NOT NULL COMMENT '1文章,2行情,3公告,4招标,5资料',
  PRIMARY KEY (`cateid`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
关于ThinkPhp5 无限极分类的记录_第1张图片
1.png
关于ThinkPhp5 无限极分类的记录_第2张图片
2.png

插入几条数据

-- ----------------------------
-- Records of yr_cate
-- ----------------------------
INSERT INTO `yr_cate` VALUES ('1', '行业动态', '0', '0,1', '1', '1');
INSERT INTO `yr_cate` VALUES ('2', '产业观察', '0', '0,2', '1', '1');
INSERT INTO `yr_cate` VALUES ('3', '行业新闻', '1', '0,1,3', '2', '1');
INSERT INTO `yr_cate` VALUES ('4', '协会新闻', '1', '0,1,4', '2', '1');
INSERT INTO `yr_cate` VALUES ('5', '名家专栏', '1', '0,1,5', '2', '1');
INSERT INTO `yr_cate` VALUES ('6', '企业新闻', '1', '0,1,6', '2', '1');
INSERT INTO `yr_cate` VALUES ('7', '行情价格', '0', '0,7', '1', '2');
INSERT INTO `yr_cate` VALUES ('8', '品种快讯', '7', '0,7,8', '2', '2');
INSERT INTO `yr_cate` VALUES ('17', '品种分析', '7', '0,7,17', '2', '1');
INSERT INTO `yr_cate` VALUES ('18', '价格指数', '7', '0,7,18', '2', '2');
INSERT INTO `yr_cate` VALUES ('20', '招标采购', '0', '0,20', '1', '4');
INSERT INTO `yr_cate` VALUES ('21', '中标公告', '20', '0,20,21', '2', '1');
INSERT INTO `yr_cate` VALUES ('22', '招标信息', '20', '0,20,22', '2', '4');
INSERT INTO `yr_cate` VALUES ('23', '资料库', '0', '0,23', '1', '5');
INSERT INTO `yr_cate` VALUES ('25', '名人名企', '0', '0,25', '1', '1');
INSERT INTO `yr_cate` VALUES ('27', '金融服务', '0', '0,27', '1', '1');

控制器

cate.php

order('cateid asc')->select(); 
        $this->assign('cateslist',$cateslist);
        return $this->fetch();
    }

    //添加分类
    public function cateadd()
    {
        //查询已添加的分类数据
        $cateslist=db('cate')->field(['*','concat(path,",",cateid)'=>'paths'])->order('paths asc')->select(); 
        
        foreach($cateslist as $k=>$v){
            $cateslist[$k]['catename']=str_repeat("|------", $v['level']).$v['catename'];
        }
        $this->assign('cateslist',$cateslist);

        //添加分类数据
        if(request()->isPost()){

            $m=model('cate');

            $data = [
                    'catename' => input('catename'),
                    'pid'      => input('pid'),
                    'model'    => input('model'),
                ];

            if($data['pid'] == 0){
                $data['level'] = 1;
                $data['path'] = '0';

                //验证数据
                $validate = validate('Cate'); 
                if(!$validate->scene('add')->check($data)){
                    $this->error($validate->getError());
                }

                if($m->save($data)){
                    $reId=$m->cateid;
                    if($reId){
                        $m->where('cateid',$reId)->update(['path'=> $data['path'].','.$reId]);
                        $this->success('更新成功','cate/cateslist');
                    }
                }else {
                    $this->error('更新失败');

                }

            }else{

                $cates=$m->find($data['pid']);
                $data['path']=$cates['path'];
                $data['level']= substr_count($data['path'],',') + 1;
               
                //验证数据
                $validate = validate('Cate'); 
                if(!$validate->scene('add')->check($data)){
                    $this->error($validate->getError());
                }
                
                $re=$m->save($data);
                if($re){
                    $reId=$m->cateid;
                    if($reId){
                        $m->where('cateid',$reId)->update(['path'=> $cates['path'].','.$reId]);
                        $this->success('更新成功','cate/cateslist');
                    }

                }else {
                    $this->error('更新失败');

                }

            }

        }
        return $this->fetch();
        
    }


    public function cate_del(){
        $id=$_GET['id'];
        $m=model('cate');
        $data=$m->where('pid','=',$id)->find();

        if($data){
            $str="该分类存在下级栏目,无法删除";
            echo json_encode($str);
        }else{
            //$re=$m->delete($id);
            $re=$m->destroy($id);
            if($re){
                echo 1;
            }
        }

    }

    //ajax获取分类信息
    public function cate_ajax(){
        $m=model('cate');
        $data=$m->field(['cateid as id','catename as name','pid'])->select();
        echo json_encode($data);
    }


}

上文cateadd为添加分类处理方法,也有删除等方法。

模板文件

cate_add.html 添加分类view文件














添加产品分类


{include file="common/_footer" /}

cateslist.html 分类列表view文件


{include file="common/_meta" /}


产品分类




{include file="common/_header" /}



{include file="common/_menu" /}


*点击分类名称,可立即删除
    {include file="common/_footer" /}

    验证脚本

      'require|max:25|unique:cate',
        ];
    
        protected $message = [
          'catename.require'  =>  '名字必须填写',
          'catename.max'      =>  '名字最多不能超过25个字符',
          'catename.unique'      =>  '栏目名字已经存在',
          'password.require'  =>  '密码必填',
    
        ];
    
        protected $scene=[
           'add' => ['catename'],
           'edit'=> ['catename'],
        ];
    
    }
    

    本文仅和本人博客www.912616.com发布,不定期整理,能力有限,存在诸多错误,谢绝转载。

    你可能感兴趣的:(关于ThinkPhp5 无限极分类的记录)