THINKPHP3.2.3含有树形结构的无限分类的实现,详细教程请访问http://gouguoyin.cn/php/6.html 1. [图片] 54fff0125903b.png 2. [代码] Category.class.php代码 ?phpnamespace Common\Helper;class Category { //一维数组 sta
1. [图片] 54fff0125903b.png
2. [代码]Category.class.php代码
<?php namespace Common\Helper; class Category { //一维数组 static public function toLevel($cate, $delimiter = '———', $parent_id = 0, $level = 0) { $arr = array(); foreach ($cate as $v) { if ($v['parent_id'] == $parent_id) { $v['level'] = $level + 1; $v['delimiter'] = str_repeat($delimiter, $level); $arr[] = $v; $arr = array_merge($arr, self::toLevel($cate, $delimiter, $v['id'], $v['level'])); } } return $arr; } //组成多维数组 static public function toLayer($cate, $name = 'child', $pid = 0){ $arr = array(); foreach ($cate as $v) { if ($v['pid'] == $pid) { $v[$name] = self::toLayer($cate, $name, $v['id']); $arr[] = $v; } } return $arr; } //一维数组(同模型)(model = tablename相同),删除其他模型的分类 static public function getLevelOfModel($cate, $tablename = 'article') { $arr = array(); foreach ($cate as $v) { if ($v['tablename'] == $tablename) { $arr[] = $v; } } return $arr; } //一维数组(同模型)(modelid),删除其他模型的分类 static public function getLevelOfModelId($cate, $modelid = 0) { $arr = array(); foreach ($cate as $v) { if ($v['modelid'] == $modelid) { $arr[] = $v; } } return $arr; } //传递一个子分类ID返回他的所有父级分类 static public function getParents($cate, $id) { $arr = array(); foreach ($cate as $v) { if ($v['id'] == $id) { $arr[] = $v; $arr = array_merge(self::getParents($cate, $v['pid']), $arr); } } return $arr; } //传递一个子分类ID返回他的同级分类 static public function getSameCate($cate, $id) { $arr = array(); $self = self::getSelf($cate, $id); if (empty($self)) { return $arr; } foreach ($cate as $v) { if ($v['id'] == $self['pid']) { $arr[] = $v; } } return $arr; } //判断分类是否有子分类,返回false,true static public function hasChild($cate, $id) { $arr = false; foreach ($cate as $v) { if ($v['pid'] == $id) { $arr = true; return $arr; } } return $arr; } //传递一个父级分类ID返回所有子分类ID /** *@param $cate 全部分类数组 *@param $pid 父级ID *@param $flag 是否包括父级自己的ID,默认不包括 **/ static public function getChildsId($cate, $pid, $flag = 0) { $arr = array(); if ($flag) { $arr[] = $pid; } foreach ($cate as $v) { if ($v['pid'] == $pid) { $arr[] = $v['id']; $arr = array_merge($arr , self::getChildsId($cate, $v['id'])); } } return $arr; } //传递一个父级分类ID返回所有子级分类 static public function getChilds($cate, $pid) { $arr = array(); foreach ($cate as $v) { if ($v['pid'] == $pid) { $arr[] = $v; $arr = array_merge($arr, self::getChilds($cate, $v['id'])); } } return $arr; } //传递一个分类ID返回该分类相当信息 static public function getSelf($cate, $id) { $arr = array(); foreach ($cate as $v) { if ($v['id'] == $id) { $arr = $v; return $arr; } } return $arr; } //传递一个分类ID返回该分类相当信息 static public function getSelfByEName($cate, $ename) { $arr = array(); foreach ($cate as $v) { if ($v['ename'] == $ename) { $arr = $v; return $arr; } } return $arr; } } ?>
3. [代码]控制器CategoryController.class
<?php /** * alltosun.com CategoryController.class.php * ============================================================================ * 版权所有 (C) 2014-2016 GoCMS内容管理系统 * 官方网站: http://www.gouguoyin.cn * 联系方式: QQ:245629560 * ---------------------------------------------------------------------------- * 许可声明:这是一个开源程序,未经许可不得将本软件的整体或任何部分用于商业用途及再发布。 * ============================================================================ * $Author: 勾国印 (phper@gouguoyin.cn) $ * $Date: 2015-3-11 下午4:00:17 $ * $Id$ */ namespace Admin\Controller; use Think\Controller; use Common\Helper\Category; class CategoryController extends AuthController { //栏目列表页 public function index(){ $category_list = M('category')->order('sort desc')->select(); $category_list = Category::toLevel($category_list, ' ',0); $this->assign('category_list', $category_list); $this->display('category_list'); } //增加 栏目页面 public function add(){ $parent_id = I('parent_id', 0); $parent_category_info = M('Category')->where(array('id' => $parent_id))->find(); if($parent_category_info){ $this->assign('parent_id', $parent_id); $this->assign('parent_category_info', $parent_category_info); } $model_list = M('Model')->where(array('status' => 1))->order('sort asc')->select(); $category_list = M('category')->order('sort desc')->select(); $category_list = Category::toLevel($category_list, '---',0); $this->assign('category_list', $category_list); $this->assign('action', 'add'); $this->assign('model_list', $model_list); $this->display('category_add'); } //修改栏目 public function edit(){ $category_id = I('id'); $category_info = M('Category')->find($category_id); $model_list = M('Model')->order('sort desc')->select(); $category_list = M('category')->order('sort desc')->select(); $category_list = Category::toLevel($category_list, '---',0); $this->assign('category_list', $category_list); $this->assign('action', 'edit'); $this->assign('category_id', $category_id); $this->assign('category_info', $category_info); $this->assign('model_list', $model_list); $this->display('category_add'); } //保存栏目 public function save(){ $Category = D("Category"); if (!$Category->create()){ //验证没有通过 输出错误提示信息 $errormsg = $Category->getError(); $this->error($errormsg); }else{ // 验证通过进行其他数据操作 $id = I('id', 0); $data['title'] = I('title', ''); $data['flag'] = I('flag', ''); $data['parent_id'] = I('parent_id', 0); $data['content'] = I('content', ''); $data['sort'] = I('sort', 0); $data['status'] = I('status', 1); $data['is_show'] = I('is_show', 1); $data['has_cover'] = I('has_cover', 1); $data['cover'] = I('cover', ''); $category_info = $Category->find($id); if($category_info){ //更新操作 $data['update_time'] = time(); $result = $Category->where(array('id' => $id))->save($data); if(false !== $result || 0 !== $result){ $this->success('栏目更新成功', U('Admin/Category/index')); }else{ $this->error('栏目更新失败'); } }else{ $data['model_id'] = I('model_id', 0); $data['model_flag'] = M('Model')->where(array('id' => I('model_id')))->getField('flag'); $data['add_time'] = time(); //print_r($data);exit; $result = $Category->add($data); if($result){ $this->success('栏目添加成功', U('Admin/Category/index')); }else{ $this->error('栏目添加失败'); } } } } //更新状态 public function change_status() { $id = I('id'); $Category = M('Category'); $category_info = $Category->find($id); if(!$category_info){ $this->ajaxReturn(array('info' => '该栏目不存在')); }else{ if($category_info['status']){ $status = 0; }else{ $status = 1; } $result = $Category->where(array('id' => $id))->setField('status',$status); if(false != $result){ $this->ajaxReturn(array('status' => 'ok', 'info' => '栏目状态更新成功')); }else{ $this->ajaxReturn(array('info' => '栏目状态更新失败')); } } } //保存排序 public function save_sort() { $id = I('id'); $sort = I('sort'); $Category = M('Category'); $result = $Category->where(array('id' => $id))->setField('sort',$sort); if(false !== $result || 0 !== $result){ $this->ajaxReturn(array('status' => 'ok', 'info' => '排序更新成功')); }else{ $this->ajaxReturn(array('info' => '排序更新失败')); } } //删除栏目 public function delete() { $id = I('id'); $Category = M('Category'); $category_info = $Category->find($id); if(!$category_info){ return array('info' => '该模型不存在'); }else if($category_info['is_system']){ return array('info' => '系统模型不允许删除'); }else{ $result = $Category->delete($id); if($result){ $this->ajaxReturn(array('status' => 'ok', 'info' => '栏目删除成功')); }else{ $this->ajaxReturn(array('info' => '栏目删除失败')); } } } }
4. [代码]前端调用
<div class="control-group "> <label>所属栏目:</label> <select name="pid"> <option value="0" {if condition="$category_info['parent_id'] eq 0"}selected{/if}>顶级栏目</option> {foreach name='category_list' item='v'} <option value="{$v.id}" {if condition="($parent_id eq $v['id']) OR ($category_id eq $v['id']) AND ($category_info['parent_id'] neq 0)"}selected{/if}>{$v.delimiter}{$v.title}</option> {/foreach} </select> <span class="Validform_checktip"></span> </div>
5. [代码]数据库
CREATE TABLE IF NOT EXISTS `go_category` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '分类id', `model_id` int(10) NOT NULL COMMENT '模型id', `model_flag` varchar(20) NOT NULL COMMENT '模型标示符', `title` varchar(45) NOT NULL COMMENT '分类名称', `flag` varchar(255) NOT NULL COMMENT '栏目标示符,必须为字母或数字', `content` mediumtext NOT NULL COMMENT '内容,仅用于单页内容', `has_cover` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否开启封面', `cover` varchar(255) NOT NULL COMMENT '分类封面图', `parent_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '父级id', `sort` int(3) unsigned NOT NULL DEFAULT '0' COMMENT '显示顺序', `is_show` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否显示,1:显示,0:不显示,默认1', `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态', `add_time` datetime NOT NULL COMMENT '添加时间', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), UNIQUE KEY `flag` (`flag`) COMMENT '标示符' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='栏目表' AUTO_INCREMENT=12 ;