Dtree.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-09-03 21:20
  12. // +----------------------------------------------------------------------
  13. class Dtree {
  14. public $ret;
  15. public $arr; // 生成树型结构所需要的2维数组
  16. public $nbsp = "&nbsp;";
  17. public $icon = array('│', '├', '└'); // 生成树型结构所需修饰符号,可以换成图片
  18. public $is_noselect = false;//是否不支持无下一级选项
  19. /**
  20. * 构造函数,初始化类
  21. * @param array 2维数组,例如:
  22. * array(
  23. * 1 => array('id'=>'1','pid'=>0,'name'=>'一级栏目一'),
  24. * 2 => array('id'=>'2','pid'=>0,'name'=>'一级栏目二'),
  25. * 3 => array('id'=>'3','pid'=>1,'name'=>'二级栏目一'),
  26. * 4 => array('id'=>'4','pid'=>1,'name'=>'二级栏目二'),
  27. * 5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'),
  28. * 6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'),
  29. * 7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二')
  30. * )
  31. */
  32. public function init($arr = array()) {
  33. $this->arr = $this->ret = NULL;
  34. $this->arr = $arr;
  35. return is_array($arr);
  36. }
  37. /**
  38. * 得到父级数组
  39. *
  40. * @param int
  41. * @return array
  42. */
  43. public function get_parent($myid) {
  44. $newarr = array();
  45. if(!isset($this->arr[$myid])) return FALSE;
  46. $pid = $this->arr[$myid]['pid'];
  47. $pid = $this->arr[$pid]['pid'];
  48. if (is_array($this->arr)) {
  49. foreach ($this->arr as $id => $a) {
  50. if ($a['pid'] == $pid) $newarr[$id] = $a;
  51. }
  52. }
  53. return $newarr;
  54. }
  55. /**
  56. * 得到子级数组
  57. *
  58. * @param int
  59. * @return array
  60. */
  61. public function get_child($myid) {
  62. $a = $newarr = array();
  63. if (is_array($this->arr)) {
  64. foreach ($this->arr as $id => $a) {
  65. if ($a['pid'] == $myid) $newarr[$id] = $a;
  66. }
  67. }
  68. return $newarr ? $newarr : FALSE;
  69. }
  70. /**
  71. * 获取子栏目json
  72. *
  73. * Enter description here ...
  74. * @param unknown_type $myid
  75. */
  76. public function creat_sub_json($myid, $str='') {
  77. $sub_cats = $this->get_child($myid);
  78. $n = 0;
  79. if(is_array($sub_cats)) foreach($sub_cats as $c) {
  80. $data[$n]['id'] = iconv(CHARSET,'utf-8',$c['catid']);
  81. if($this->get_child($c['catid'])) {
  82. $data[$n]['liclass'] = 'hasChildren';
  83. $data[$n]['children'] = array(array('text'=>'&nbsp;','classes'=>'placeholder'));
  84. $data[$n]['classes'] = 'folder';
  85. $data[$n]['text'] = iconv(CHARSET,'utf-8',$c['catname']);
  86. } else {
  87. if($str) {
  88. @extract(array_iconv($c,CHARSET,'utf-8'));
  89. eval("\$data[$n]['text'] = \"$str\";");
  90. } else {
  91. $data[$n]['text'] = iconv(CHARSET,'utf-8',$c['catname']);
  92. }
  93. }
  94. $n++;
  95. }
  96. return json_encode($data);
  97. }
  98. private function have($list,$item){
  99. return(strpos(',,'.$list.',',','.$item.','));
  100. }
  101. }