建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 网站开发 > PHP教程 >

php实现无限级分类

时间:2009-04-22 23:11来源: 作者: 点击:
php实现无限级分类
  1. <?php   
  2. /**   
  3. * author: askie   
  4. * blog: http://www.pkphp.com   
  5. * 版权: 随便用   
  6. * 无限分类   
  7. */   
  8. class Tree   
  9. {   
  10. public $data=array();   
  11. public $cateArray=array();   
  12.  
  13. function Tree()   
  14. {   
  15.  
  16. }   
  17. function setNode ($id, $parent, $value)   
  18. {   
  19. $parent = $parent?$parent:0;   
  20. $this->data[$id] = $value;   
  21. $this->cateArray[$id] = $parent;   
  22. }   
  23. function getChildsTree($id=0)   
  24. {   
  25. $childs=array();   
  26. foreach ($this->cateArray as $child=>$parent)   
  27. {   
  28. if ($parent==$id)   
  29. {   
  30. $childs[$child]=$this->getChildsTree($child);   
  31. }   
  32.  
  33. }   
  34. return $childs;   
  35. }   
  36. function getChilds($id=0)   
  37. {   
  38. $childArray=array();   
  39. $childs=$this->getChild($id);   
  40. foreach ($childs as $child)   
  41. {   
  42. $childArray[]=$child;   
  43. $childArray=array_merge($childArray,$this->getChilds($child));   
  44. }   
  45. return $childArray;   
  46. }   
  47. function getChild($id)   
  48. {   
  49. $childs=array();   
  50. foreach ($this->cateArray as $child=>$parent)   
  51. {   
  52. if ($parent==$id)   
  53. {   
  54. $childs[$child]=$child;   
  55. }   
  56. }   
  57. return $childs;   
  58. }   
  59. //单线获取父节点   
  60. function getNodeLever($id)   
  61. {   
  62. $parents=array();   
  63. if (key_exists($this->cateArray[$id],$this->cateArray))   
  64. {   
  65. $parents[]=$this->cateArray[$id];   
  66. $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));   
  67. }   
  68. return $parents;   
  69. }   
  70. function getLayer($id,$preStr=’|-’)   
  71. {   
  72. return str_repeat($preStr,count($this->getNodeLever($id)));   
  73. }   
  74. function getValue ($id)   
  75. {   
  76. return $this->data[$id];   
  77. } // end func   
  78. }   
  79.  
  80. $Tree = new Tree(”请选择分类”);   
  81. //setNode(目录ID,上级ID,目录名字);   
  82. $Tree->setNode(1, 0, ‘目录1′);   
  83. $Tree->setNode(2, 1, ‘目录2′);   
  84. $Tree->setNode(5, 3, ‘目录5′);   
  85. $Tree->setNode(3, 0, ‘目录3′);   
  86. $Tree->setNode(4, 2, ‘目录4′);   
  87. $Tree->setNode(9, 4, ‘目录9′);   
  88. $Tree->setNode(6, 2, ‘目录6′);   
  89. $Tree->setNode(7, 2, ‘目录7′);   
  90. $Tree->setNode(8, 3, ‘目录8′);   
  91.  
  92. //print_r($Tree->getChildsTree(0));   
  93. //print_r($Tree->getChild(0));   
  94. //print_r($Tree->getLayer(2));   
  95.  
  96. $category = $Tree->getChilds();   
  97.  
  98. //遍历输出   
  99. foreach ($category as $key=>$id)   
  100. {   
  101. echo $id.$Tree->getLayer($id, ‘|-’).$Tree->getValue($id).”\n”;   
  102. }   
  103.  
  104. ?>  
(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片