蝙蝠岛资源网 Design By www.hbtsch.com
本文实例讲述了PHP实现的无限分类类库定义与用法。分享给大家供大家参考,具体如下:
/*
功能:基于TP2.0的无限分类。
用法:
第一种用法,不采用数据库,可以不需要TP,例子如下
<"";
//格式化的字符
private $icon = array(' │', ' ├ ', ' └ ');
//字段映射,分类id,上级分类pid,分类名称title,格式化后分类名称fulltitle
private $field = array();
/*
功能:构造函数,对象初始化;
属性:public;
参数:$model,数组或对象,基于TP2.0的数据表模型名称,若不采用TP2.0,可传递空值。
$field,字段映射,分类id,上级分类pid,分类名称title,格式化后分类名称fulltitle
依次传递,例如在分类数据表中,分类id,字段名为CatID,上级分类pid,字段名称name,希望格式化分类后输出cname,
则,传递参数为,$field('CatID','pid','name','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'] "")
{
//下级分类的数组
$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 "";
}
$childs[$i][$this->field['fulltitle']] = ($space "") . $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程序设计算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无PHP实现的无限分类类库定义与用法示例【基于thinkPHP】的评论...
更新日志
2025年10月29日
2025年10月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]