栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP实现的无限分类类库定义与用法示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP实现的无限分类类库定义与用法示例

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


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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/39554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号