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

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

rush:PHP;"> /* 功能:基于TP2.0的无限分类用法: 第一种用法,不采用数据库,可以不需要TP,例子如下 1,'pid'=>0,'name'=>'中国'); $data[]=array('cat_id'=>2,'name'=>'美国'); $data[]=array('cat_id'=>3,'name'=>'韩国'); $data[]=array('cat_id'=>4,'pid'=>1,'name'=>'北京'); $data[]=array('cat_id'=>5,'name'=>'上海'); $data[]=array('cat_id'=>6,'name'=>'广西'); $data[]=array('cat_id'=>7,'pid'=>6,'name'=>'桂林'); $data[]=array('cat_id'=>8,'name'=>'南宁'); $data[]=array('cat_id'=>9,'name'=>'柳州'); $data[]=array('cat_id'=>10,'pid'=>2,'name'=>'纽约'); $data[]=array('cat_id'=>11,'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'].'
'; } 第二种用法,采用数据库,基于TP,例子如下 数据表,前缀_articlec_cat,包含cat_id,pid,title三个字段 require('Category.class.PHP');//导入Category.class.PHP类 $cat=new Category('ArticleCat','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; 参数:$model,数组或对象,基于TP2.0的数据表模型名称,若不采用TP2.0,可传递空值。 $field,字段映射,分类id,上级分类pid,格式化后分类名称fulltitle 依次传递,例如在分类数据表中,分类id,字段名为CatID,上级分类pid,字段名称name,希望格式化分类输出cname,则,传递参数为,$field('CatID','cname');若为空,则采用认值。 返回:无 备注:用到了TP的D函数 */ 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; 参数:查询条件$condition; 返回:无; 备注:需要TP支持 */ 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(); } } /* 功能:返回给定上级分类$pid的所有同一级子分类属性:public; 参数:上级分类$pid; 返回:子分类,二维数组; 备注: */ public function getChild($pid) { $childs = array(); foreach ($this->rawList as $Category) { if ($Category[$this->field['pid']] == $pid) $childs[] = $Category; } return $childs; } /* 功能:无限分类核心部分,递归格式化分类前的字符; 属性:private; 参数:分类id,前导空格; 返回:无; 备注: */ 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; 参数:$condition,数字或字符串,需要符合TP查询条件规则,起始分类id,$CatID=0; $orderby 排序参数 返回:递归格式化分类数组; 备注:需要TP支持 */ 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; 参数:$data,二维数组,起始分类id,认$CatID=0; 返回:递归格式化分类数组; 备注: */ public function getTree($data,$CatID = 0) { unset($this->rawList); unset($this->formatList); $this->rawList = $data; $this->_searchList($CatID); return $this->formatList; } /* 功能获取错误信息; 属性:public; 参数:无; 返回:错误信息字符串; 备注: */ public function getError() { return $this->error; } /* 功能:检查分类参数$CatID,是否为空; 属性:private; 参数:分类参数$CatID,整型。 返回:正确,返回true,错误,返回false; 备注: */ private function _checkCatID($CatID) { if (intval($CatID)) { return true; } else { $this->error = "参数分类ID为空或者无效!"; return false; } } /* 功能查询路径; 属性:private; 参数:分类参数$CatID,整型。 返回:出错返回false; 备注:需要TP支持 */ 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']]); } /* 功能查询给定分类id的路径; 属性:public; 参数:分类参数$CatID,整型。 返回:数组; 备注:需要TP支持 */ public function getPath($CatID) { unset($this->rawList); unset($this->formatList); //查询分类路径 $this->_searchPath($CatID); return array_reverse($this->formatList); } /* * **************************************以下为分类添加修改删除*************************************************** */ /* 功能添加分类属性:public; 参数:$data,一维数组,要添加的数据,$data需要包含上级分类ID。 返回:添加成功,返回相应的分类ID,添加失败,返回FALSE; 备注:需要TP支持 */ public function add($data) { if (empty($data)) return false; return $this->model->data($data)->add(); } /* 功能修改分类属性:public; 参数:$data,一维数组,传递编辑的数据,$data需要包含要修改分类ID。 返回:修改成功,返回相应的分类ID,修改失败,返回FALSE; 备注:需要TP支持 */ public function edit($data) { if (empty($data)) return false; return $this->model->data($data)->save(); } /* 功能删除分类属性:public; 参数:分类ID 返回:删除成功,返回相应的分类ID,删除失败,返回FALSE; 备注:需要TP支持 */ 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程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_form` (   `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string)、复合查询(_complex)、请求字符串查询(_query),混合查询中的特殊查询每次查询只能定义一个,由于采用数组的
(1)创建模版:/App/Home/View/Form/edit.html   <FORM method=\"post\" action=\"__URL__/update\">
自定义配置文件user.php: <?php return array(    \'sex\'=>\'man\', ); config.php: <?php return array(
在一些成熟的CMS系统中,后台一般都包含一个配置中心(如织梦后台中系统设置),以方便站长在后台修改配置文件;那么这个功能是如果实现的呢?在ThinkPHP中有没有捷径可走呢?答案肯定是有的。下面大概说一下这个功能
废话不多说先上图预览下,即本博客的分页; 这个分页类是在thinkphp框架内置的分页类的基础上修改而来,原分页类的一些设计,在实际运用中感觉不是很方便;
在php中截取字符串的函数有很多,而在thinkphp中也可以直接使用php的函数,本文给大家简单的介绍thinkPHP模板中截取字符串的具体用法,希望能对各位有所帮助。
thinkphp开发图片上传,图片异步上传是目前比较方便的功能,这里我就不写css文件了,将代码写出来。
配置数据库:/app/Common/Conf/config.php 方法一: // 添加数据库配置信息 \'DB_TYPE\'   => \'mysql\',// 数据库类型
/app/Home/Controller/IndexController.class.php
(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_data` (   `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
(1)控制器设置:/app/Home/Controller/IndexController.class.php <?php namespace HomeController; use ThinkController;
(1)普通模式 http://localhost/index.php?m=module&a=action&var=value m参数表示模块,a操作表示操作(模块和操作的URL参数名称是可以配置的),后面的表示其他GET参数。
入库的时候用htmlspecialchars()处理含有html代码的内容 输出的时候用htmlspecialchars_decode()处理含有html代码的内容
<?php define(\'APP_NAME\',\'app\'); define(\'APP_PATH\',\'./app/\'); define(\'APP_DEBUG\',TRUE); // 开启调试模式
(1)创建控制器中定义read方法:/App/Home/Controller/FormController.class.php public function read($id=0){
一、实现不同字段相同的查询条件 $User = M(\"User\"); // 实例化User对象 $map[\'name|title\'] = \'thinkphp\';
如果你的数据完全是内部操作写入而不是通过表单的话(也就是说可以充分信任数据的安全),那么可以直接使用add方法,如:
查询表达式的使用格式: $map[\'字段名\'] = array(\'表达式\',\'查询条件\'); 表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是:
一、使用字符串作为查询条件 $User = M(\"User\"); // 实例化User对象 $User->where(\'type=1 AND status=1\')->select();