Thinkphp3.2.3 ----后台----无限极分类

本文详细介绍了如何在商品分类系统中实现无限级分类,包括数据库设计、递归算法的应用及在MVC架构中的具体实现过程。

先来对数据表进行分析

 FieldTypeComment
catIdint(11) NOT NULL 
 parentIdint(11) NOT NULL 
 isShowtinyint(4) NOT NULL 
 catNamevarchar(20) NOT NULL 
 priceSectiontext NULL 
 catSortint(11) NOT NULL 
 catFlagtinyint(4) NOT NULL 
 isFloortinyint(4) NOT NULL

parentId:表示当前分类的父 id,他是实现无限级分类的关键

无限级分类,主要从两个方面进行考虑的。

1, 数据库的设计,表中的 parentId 字段。

2, 在程序的层面上,来完成,使用递归。

说白了也就是 parentId  就是他的老爸的 ID 一直到 parentId 为 0 这就到了顶级分类


添加分类,首先写入 Model 层

//新增
public function added() {
    $goodscategoryInfo = $this->create();
    if( !empty( $goodscategoryInfo ) ) {
        $res = $this->add( $goodscategoryInfo );
        if( $res !== false ) {
            return true;
        }else {
            return false;
        }
    }else {
        return false;
    }
}

然后是写入 Controller 层

//添加
public function add() {
    $this->assign( 'goodscategoryList', D( 'goods_category' )->getlist() );
    $this->assign( "act", U( "insert" ) );
    $this->assign( "actInfo", "添加" );
    $this->display( "goods_category_info" );
}

public function insert() {
    $res = D( "goods_category" )->added();
    if( $res !== false ) {
        $this->success( "添加成功" );
    }else {
        $this->error( "添加失败" );
    }
}

最后在写入 View 层

<form class="form-horizontal" role="form" method="post" action="{$act}">
    <div class="form-group">
        <label for="firstname" class="col-sm-2 control-label">上级商品分类名称</label>
        <div class="col-sm-6">
            <select name="parentId">
                <option value="0">顶级分类</option>
                <volist name="goodscategoryList" id='vo'>
                    <option value="{$vo['catid']}">{$vo.nbsp}{$vo['catname']}</option>
                </volist>
            </select>
        </div>
    </div>



好啦!完成添加功能,现在到了显示,首先写入 Model 层

//列表
public function getlist() {
    $cats = $this->select();
    //获取树状的分类
    return $this->getTree($cats);
}


//创建组织结构分类树(无限极分类)
public function getTree( &$list, $parentId = 0, $level = 0, $nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;' ) {
    static $tree = array();
    foreach( $list as $v ) {
        if( $v['parentid'] == $parentId ) {
            //说明找到,保存
            $v['nbsp'] = str_repeat( $nbsp, $level );
            $tree[] = $v;
            //继续找
            $this->getTree( $list, $v['catid'], $level + 1 );
        }
    }
    return $tree;
}

然后是写入 Controller 层

//获取列表
public function index() {
    $this->assign( 'goodscategoryList', D( 'goods_category' )->getlist() );
    $this->display( 'goods_category_list' );
}
最后在写入 View 层

<td>{$vo.nbsp}{$vo.catname}</td>
最终目的要成为这样

商品分类名称 排序号 是否显示 操作
时蔬水果、网上菜场 1 显示   
    进口水果 0 显示   
        橙柚 0 显示   
        苹果 0 显示   
        凤梨 1 显示   
        火龙果 4 显示   
        梨 6 显示   
        芒果 8 显示   
        蓝莓 12 显示   


好啦!完成显示功能,现在到了更新,首先写入 Model 层

//更新
public function update() {
    $goodscategoryInfo = $this->create();
    if( !empty( $goodscategoryInfo ) ) {
        $res = $this->save( $goodscategoryInfo );
        if( $res !== false ) {
            return true;
        }else {
            return false;
        }
    }else {
        return false;
    }
}
然后是写入 Controller 层

//编辑
public function edit() {
    $this->assign( "goodscategoryList", D( 'goods_category' )->getlist() );
    $this->assign( "goodscategoryInfo", D( 'goods_category' )->get() );
    $this->assign( "act", U( "update" ) );
    $this->assign( "actInfo", "更新" );
    $this->display( "goods_category_info" );
}

public function update() {
    $res = D( "goods_category" )->update();
    if( $res !== false ) {
        $this->success( "更新成功" );
    }else {
        $this->error( "更新失败" );
    }
}
最后在写入 View 层

<form class="form-horizontal" role="form" method="post" action="{$act}">
    <div class="form-group">
        <label for="firstname" class="col-sm-2 control-label">上级商品分类名称</label>
        <div class="col-sm-6">
            <select name="parentId">
                <option value="0">顶级分类</option>
                <volist name="goodscategoryList" id='vo'>
                    <option value="{$vo['catid']}">{$vo.nbsp}{$vo['catname']}</option>
                </volist>
            </select>
        </div>
    </div>



好啦!完成更新功能,现在到了删除,首先写入 Model 层

//删除
public function delete() {
    return $this->delete( I( "get.catid" ) );
}
然后是写入 Controller 层

//删除
public function delete() {
    $res = M( 'goods_category' )->delete( I( "get.catid" ) );
    if( $res !== false ) {
        $this->success( "删除成功" );
    }else {
        $this->error( "删除失败" );
    }
}


注意:编辑的表单中一定要传 catId ,否则更新会失败

<input type="hidden" name="catId" value="<notempty name='goodscategoryInfo.catid'>{$goodscategoryInfo.catid}</notempty>">


此无限极分类功能存在两个问题

1,添加子类需要手动选择上级。

2,删除子类会同时删除子类下的子级。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值