tree.init.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.2 国产PHP开发框架 扩展类库-无限极分类
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class treeInit {
  12. private $parentid = 'parentid';
  13. private $id = 'id';
  14. private $name = 'name';
  15. /**
  16. * 无限级分类树-初始化配置
  17. * @param array $config array('parentid'=>'', 'id' => '', 'name' =>'name')
  18. * @return string|array
  19. */
  20. public function init($config = array()) {
  21. if (!is_array($config)) return false;
  22. $this->parentid = (isset($config['parentid'])) ? $config['parentid'] : $this->parentid;
  23. $this->id = (isset($config['id'])) ? $config['id'] : $this->id;
  24. $this->name = (isset($config['name'])) ? $config['name'] : $this->name;
  25. return true;
  26. }
  27. /**
  28. * 无限级分类树-获取树
  29. * @param array $tree 树的数组
  30. * @param int $mid 初始化树时候,代表ID下的所有子集
  31. * @param int $selectid 选中的ID值
  32. * @param string $code 代码
  33. * @param string $prefix 前缀
  34. * @param string $selected 选中
  35. * @return string|array
  36. */
  37. public function get_tree($tree, $mid = 0, $selectid = 5, $code = "<option value='\$id' \$selecteds>\$prefix\$name</option>", $prefix = '|-', $selected = 'selected') {
  38. if (!is_array($tree)) return '';
  39. $temp = array();
  40. $string = '';
  41. foreach ($tree as $k => $v) {
  42. if ($v[$this->parentid] == $mid) {
  43. $id = $v[$this->id];
  44. $name = $v[$this->name];
  45. $selecteds = ($id == $selectid) ? $selected : '';
  46. eval("\$temp_code = \"$code\";");//转化
  47. $string .= $temp_code;
  48. $string .= $this->get_tree($tree, $v[$this->id], $selectid, $code, '&nbsp;&nbsp;' . $prefix);
  49. }
  50. }
  51. return $string ;
  52. }
  53. /**
  54. * 无限级分类树-获取子类
  55. * @param array $tree 树的数组
  56. * @param int $id 父类ID
  57. * @return string|array
  58. */
  59. public function get_child($tree, $id) {
  60. if (!is_array($tree)) return array();
  61. $temp = array();
  62. foreach ($tree as $k => $v) {
  63. if ($v[$this->parentid] == $id) {
  64. $temp[] = $v;
  65. }
  66. }
  67. return $temp;
  68. }
  69. /**
  70. * 无限级分类树-获取父类
  71. * @param array $tree 树的数组
  72. * @param int $id 子类ID
  73. * @return string|array
  74. */
  75. public function get_parent($tree, $id) {
  76. if (!is_array($tree)) return array();
  77. $temp = array();
  78. foreach ($tree as $k => $v) {
  79. $temp[$v[$this->id]] = $v;
  80. }
  81. $parentid = $temp[$id][$this->parentid];
  82. return $temp[$parentid];
  83. }
  84. }