码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp无限分类模块实现

时间:2020-05-10 21:28:25      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:add   col   ati   end   href   app   分割线   ace   sign   

数据表结构如下:

技术图片

 

 

控制器核心代码:

<?php
namespace app\index\controller;
use think\Controller;

class Goods extends Controller
{
    public function product_category()
    {

        return $this->fetch();
    }

    public function product_category_add()
    {
        $m = Model(‘goods_type‘);
        // 根据paths进行排序
        // \对,进行转义,因为本身是作为分隔符使用
        $data = $m->field("*,concat(path,‘\,‘,id) as paths")->order(‘paths‘)->select();
        foreach($data as $k=>$v){
            //根据level的值,决定需要循环几次,生成分割线
            $v[‘name‘] = str_repeat("|------", $v[‘level‘]).$v[‘name‘];
        }
        $this->assign(‘data‘, $data);

        return $this->fetch();
    }

    //添加分类
    public function goods_type_add(){
        if($_POST[‘name‘] === ‘‘){
            echo ‘<script>alert("添加失败,分类名不能为空");parent.location.href="product_category";</script>‘;
        }

        //var_dump($_POST);
        $data[‘name‘] = $_POST[‘name‘];
        $data[‘pid‘] = $_POST[‘pid‘];
        $m = Model(‘goods_type‘);

        if($data[‘pid‘] === "0"){
            //如果是一级分类
            $data[‘level‘] = 1;
            //新增分类的id
            $info = $m->where(‘1=1‘)->order(‘id desc‘)->find();
            $newId = $info[‘id‘]+1;
            $data[‘path‘] = ‘0,‘.$newId;
        }else{
            $pinfo = $m->field(‘path,level‘)->where(‘id‘,$data[‘pid‘])->select();
            //查找父分类的path和level
            $data[‘level‘] = $pinfo[0][‘level‘]+1;

            //新增分类的id
            $info = $m->where(‘1=1‘)->order(‘id desc‘)->find();
            $newId = $info[‘id‘]+1;
            $data[‘path‘] = $pinfo[0][‘path‘].‘,‘.$newId;
        }

        $res = $m->data($data)->save();
        if($res){
            echo ‘<script>alert("添加成功");parent.location.href="product_category";</script>‘;
        }else{
            echo ‘<script>alert("添加失败");parent.location.href="product_category";</script>‘;
        }
    }

    //获取分类数据
    public function product_category_ajax(){
        $m = model(‘goods_type‘);
        $data = $m->field(‘id,pid,name‘)->select();
        echo json_encode($data);
    }

    //删除分类数据
    public function product_category_del(){
        $id = $_GET[‘id‘];
        $m = model(‘goods_type‘);
        $res = $m->where(‘pid‘,$id)->find();
        if($res){
            $str = ‘该分类下还有子分类,不允许直接删除‘;
            echo json_encode($str);
        }else{
            $result = $m->where(‘id‘,$id)->delete();
            if($result){
                echo 1;
            }else{
                $str = ‘删除失败‘;
                echo json_encode($str);
            }
        }
    }
}

 

实现效果

技术图片

 

thinkphp无限分类模块实现

标签:add   col   ati   end   href   app   分割线   ace   sign   

原文地址:https://www.cnblogs.com/chenyingying0/p/12864774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!