PHP实现的无限分类类库定义与用法示例【基于thinkPHP】

时间:2021-05-26

本文实例讲述了PHP实现的无限分类类库定义与用法。分享给大家供大家参考,具体如下:

/*功能:基于TP2.0的无限分类。用法:第一种用法,不采用数据库,可以不需要TP,例子如下<?phprequire('Category.class.php');//导入Category.class.php类//测试数据$data[]=array('cat_id'=>1,'pid'=>0,'name'=>'中国');$data[]=array('cat_id'=>2,'pid'=>0,'name'=>'美国');$data[]=array('cat_id'=>3,'pid'=>0,'name'=>'韩国');$data[]=array('cat_id'=>4,'pid'=>1,'name'=>'北京');$data[]=array('cat_id'=>5,'pid'=>1,'name'=>'上海');$data[]=array('cat_id'=>6,'pid'=>1,'name'=>'广西');$data[]=array('cat_id'=>7,'pid'=>6,'name'=>'桂林');$data[]=array('cat_id'=>8,'pid'=>6,'name'=>'南宁');$data[]=array('cat_id'=>9,'pid'=>6,'name'=>'柳州');$data[]=array('cat_id'=>10,'pid'=>2,'name'=>'纽约');$data[]=array('cat_id'=>11,'pid'=>2,'name'=>'华盛顿');$data[]=array('cat_id'=>12,'pid'=>3,'name'=>'首尔');$cat=new Category('',array('cat_id','pid','name','cname'));$s=$cat->getTree($data);//获取分类数据树结构//$s=$cat->getTree($data,1);获取pid=1所有子类数据树结构foreach($s as $vo){echo $vo['cname'].'<br>';}第二种用法,采用数据库,基于TP,例子如下数据表,前缀_articlec_cat,包含cat_id,pid,title三个字段require('Category.class.php');//导入Category.class.php类$cat=new Category('ArticleCat',array('cat_id','pid','title','fulltitle'));$s=$cat->getList();//获取分类结构$s=$cat->getList('',1);//获取pid=1的子分类结构$s=$cat->getList($condition,1);//$condition为查询条件,获取pid=1的子分类结构$s=$cat->getPath(3);//获取分类id=3的路径$s=$cat->add($data);//添加分类,$data需要包含上级分类pid$s=$cat->edit($data);//修改分类,$data需要包含分类ID$s=$cat->del(10);//删除分类id=10的分类详细用法:参考代码说明/**+------------------------------------------------------------------------------* 分类管理+------------------------------------------------------------------------------*/class Category{ //分类的数据表模型 private $model; //原始的分类数据 private $rawList = array(); //格式化后的分类 private $formatList = array(); //错误信息 private $error = ""; //格式化的字符 private $icon = array(' │', ' ├ ', ' └ '); //字段映射,分类id,上级分类pid,分类名称title,格式化后分类名称fulltitle private $field = array(); public function __construct($model = '', $field = array()) { //判断参数类型 if (is_string($model) && (!empty($model))) { if (!$this->model = D($model)) //注意这里的D函数需要TP支持 $this->error = $model . "模型不存在!"; } if (is_object($model)) { $this->model =& $model; } $this->field['id'] = $field['0'] ? $field['0'] : 'id'; $this->field['pid'] = $field['1'] ? $field['1'] : 'pid'; $this->field['title'] = $field['2'] ? $field['2'] : 'title'; $this->field['fulltitle'] = $field['3'] ? $field['3'] : 'fulltitle'; } private function _findAllCat($condition, $orderby = NULL) { if (empty($orderby)) { $this->rawList = $this->model->where($condition)->findAll(); } else { $this->rawList = $this->model->where($condition)->order($orderby)->findAll(); } } public function getChild($pid) { $childs = array(); foreach ($this->rawList as $Category) { if ($Category[$this->field['pid']] == $pid) $childs[] = $Category; } return $childs; } private function _searchList($CatID = 0, $space = "") { //下级分类的数组 $childs = $this->getChild($CatID); //如果没下级分类,结束递归 if (!($n = count($childs))) return; $cnt = 1; //循环所有的下级分类 for ($i = 0; $i < $n; $i++) { $pre = ""; $pad = ""; if ($n == $cnt) { $pre = $this->icon[2]; } else { $pre = $this->icon[1]; $pad = $space ? $this->icon[0] : ""; } $childs[$i][$this->field['fulltitle']] = ($space ? $space . $pre : "") . $childs[$i][$this->field['title']]; $this->formatList[] = $childs[$i]; //递归下一级分类 $this->_searchList($childs[$i][$this->field['id']], $space . $pad . " "); $cnt++; } } public function getList($condition = NULL, $CatID = 0, $orderby = NULL) { unset($this->rawList); unset($this->formatList); $this->_findAllCat($condition, $orderby, $orderby); $this->_searchList($CatID); return $this->formatList; } public function getTree($data, $CatID = 0) { unset($this->rawList); unset($this->formatList); $this->rawList = $data; $this->_searchList($CatID); return $this->formatList; } public function getError() { return $this->error; } private function _checkCatID($CatID) { if (intval($CatID)) { return true; } else { $this->error = "参数分类ID为空或者无效!"; return false; } } private function _searchPath($CatID) { //检查参数 if (!$this->_checkCatID($CatID)) return false; //初始化对象,查找上级Id; $rs = $this->model->find($CatID); //保存结果 $this->formatList[] = $rs; $this->_searchPath($rs[$this->field['pid']]); } public function getPath($CatID) { unset($this->rawList); unset($this->formatList); //查询分类路径 $this->_searchPath($CatID); return array_reverse($this->formatList); } /* * **************************************以下为分类添加、修改、删除*************************************************** */ public function add($data) { if (empty($data)) return false; return $this->model->data($data)->add(); } public function edit($data) { if (empty($data)) return false; return $this->model->data($data)->save(); } public function del($CatID) { $CatID = intval($CatID); if (empty($CatID)) return false; $conditon[$this->field['id']] = $CatID; return $this->model->where($conditon)->delete(); }}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章