tree.init.php 3.0 KB

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