[PHP] 无限极分类 →→→→→进入此内容的聊天室

来自 , 2020-11-19, 写在 PHP, 查看 153 次.
URL http://www.code666.cn/view/334467d4
  1. class CategoryAction extends CommonAction{
  2.  
  3.     public function index(){
  4.                 // echo APP_PATH;
  5.                 $cate = M('cate')->order('sort ASC')->select();
  6.                 import('Class.Category',APP_PATH);
  7.                 $cate = Category::unlimitedForLevel($cate);
  8.                 $this->cate = $cate;
  9.                 $this->display();
  10.     }
  11. }
  12.  
  13.  
  14. //封装
  15. <?php
  16.  
  17. Class Category {
  18.  
  19.         //组合一维数组
  20.         Static Public function unlimitedForLevel( $cate, $html = '--', $pid = 0, $level = 0){
  21.                 $arr = array();
  22.                 foreach ($cate as $v) {
  23.                         if ($v['pid'] == $pid) {
  24.                                 $v['level'] = $level + 1;
  25.                                 $v['html'] = str_repeat($html,$level);
  26.                                 $arr[] = $v;
  27.                                 $arr = array_merge($arr, self::unlimitedForLevel($cate, $html, $v['id'], $level + 1));
  28.                         }
  29.                 }
  30.                 return $arr;
  31.         }
  32.  
  33.         //多维数组递归
  34.         Static Public function unlimitedForLayer ($cate, $name = 'child', $pid = 0) {
  35.                 $arr = array();
  36.                 foreach ($cate as $v) {
  37.                         if ($v['pid'] == $pid) {
  38.                                 $v[$name] = self::unlimitedForLayer($cate, $name, $v['id']);
  39.                                 $arr[] = $v;
  40.                         }
  41.                 }
  42.                 return $arr;
  43.         }
  44.  
  45.        
  46.         //传递一个子分类ID返回所有的父级分类ID
  47.         Static Public function getParentsId ($cate, $id) {
  48.                 $arr = array();
  49.                 foreach ($cate as $v) {
  50.                         if ($v['id'] == $id) {
  51.                                 $arr[] = $v['id'];
  52.                                 $arr = array_merge(self::getParentsId($cate, $v['pid']), $arr);
  53.                         }
  54.                 }
  55.                 return $arr;
  56.         }
  57.  
  58.         //传递一个子分类ID返回所有的父级分类
  59.         Static Public function getParents ($cate, $id) {
  60.                 $arr = array();
  61.                 foreach ($cate as $v) {
  62.                         if ($v['id'] == $id) {
  63.                                 $arr[] = $v;
  64.                                 $arr = array_merge(self::getParents($cate, $v['pid']), $arr);
  65.                         }
  66.                 }
  67.                 return $arr;
  68.         }
  69.  
  70.         //传递一个父级分类ID返回所有的子分类ID
  71.         Static Public function getChildrenId ($cate, $pid) {
  72.                 $arr = array();
  73.                 foreach ($cate as $v) {
  74.                         if ($v['pid'] == $pid) {
  75.                                 $arr[] = $v['id'];
  76.                                 $arr = array_merge($arr, self::getChildrenId($cate, $v['id']));
  77.                         }
  78.                 }
  79.                 return $arr;
  80.         }
  81.  
  82.         //传递一个父级分类ID返回所有的子分类
  83.         Static Public function getChildren ($cate, $pid) {
  84.                 $arr = array();
  85.                 foreach ($cate as $v) {
  86.                         if ($v['pid'] == $pid) {
  87.                                 $arr[] = $v;
  88.                                 $arr = array_merge($arr, self::getChildrenId($cate, $v['id']));
  89.                         }
  90.                 }
  91.                 return $arr;
  92.         }
  93.        
  94.  
  95. }
  96. ?>

回复 "无限极分类"

这儿你可以回复上面这条便签

captcha