网站地图    收藏   

主页 > php专栏 > php应用 >

PHP 查询多级分类的实例程序代码 - php高级应用

来源:自学PHP网    时间:2014-11-27 22:16 作者: 阅读:

[导读] 分类表,比如category,字段有 id,parentid,title,查询时,我们希望得到有层级关系的数组,就是顶级是顶级分类,然后每个分类中有个children子数组,记录它的子分类,这样一级一级的分级数组,代码如...

PHP 查询多级分类的实例程序代码

分类表,比如category,字段有 id,parentid,title,查询时,我们希望得到有层级关系的数组,就是顶级是顶级分类,然后每个分类中有个children子数组,记录它的子分类,这样一级一级的分级数组,代码如下:

  1. //查询 
  2.  
  3. $dsql->SetQuery("SELECT * FROM category ORDER BY sortorder ASC"); 
  4. $dsql->Execute('parentlist'); 
  5. $array = array(); 
  6. $parentlist = array(); 
  7. while ($rs=$dsql->getObject('parentlist')) 
  8.     if($rs->parentid == 0) 
  9.     { 
  10.         $parentlist[$rs->id] = (array)$rs
  11.     } 
  12.     else 
  13.     { 
  14.         $array[$rs->id] = (array)$rs
  15.     } 
  16. $parentlist = cat_options($parentlist$array);  //我们求的结果数组 
  17.  
  18. //$list父级分类的数组 
  19.  
  20. //$array是除父级分类外的全部分类的数组 
  21.  
  22. function cat_options(&$list,&$array
  23.     foreach ($list as $key => $arr
  24.     { 
  25.         foreach ($array as $k => $value
  26.         { 
  27.             if($value['parentid'] == $arr['id']) 
  28.             { 
  29.                 $list[$key]['children'][] = $value
  30.                 unset($array[$k]); 
  31.             } 
  32.         } 
  33.     } 
  34.     foreach ($list as $key => $arr
  35.     { 
  36.         if(is_array($arr['children']) && count($arr['children']) > 0) 
  37.         { 
  38.             $list[$key]['children'] = cat_options($list[$key]['children'], $array); 
  39.         } 
  40.     } 
  41.     return $list

好了现在给大家推荐一个无限分类的函数,代码如下:

  1. <?php     
  2. //模拟PHP无限分类查询结果     
  3. return array(     
  4. array(     
  5. ‘id’=>1,     
  6. ‘pid’=>0,     
  7. ‘name’=>‘主页’     
  8. ),     
  9. array(     
  10. ‘id’=>2,     
  11. ‘pid’=>0,     
  12. ‘name’=>‘新闻’     
  13. ),     
  14. array(     
  15. ‘id’=>3,     
  16. ‘pid’=>0,     
  17. ‘name’=>‘媒体’     
  18. ),     
  19. array(     
  20. ‘id’=>4,     
  21. ‘pid’=>0,     
  22. ‘name’=>‘下载’     
  23. ),     
  24. array(     
  25. ‘id’=>5,     
  26. ‘pid’=>0,     
  27. ‘name’=>‘关于我们’     
  28. ),     
  29. array(     
  30. ‘id’=>6,     
  31. ‘pid’=>2,     
  32. ‘name’=>‘天朝新闻’     
  33. ),     
  34. array(     
  35. ‘id’=>7,     
  36. ‘pid’=>2,     
  37. ‘name’=>‘海外新闻’     
  38. ),     
  39. array(     
  40. ‘id’=>8,     
  41. ‘pid’=>6,     
  42. ‘name’=>‘州官新闻’     
  43. ),     
  44. array(     
  45. ‘id’=>9,     
  46. ‘pid’=>3,     
  47. ‘name’=>‘音乐’     
  48. ),     
  49. array(     
  50. ‘id’=>10,     
  51. ‘pid’=>3,     
  52. ‘name’=>‘电影’     
  53. ),     
  54. array(     
  55. ‘id’=>11,     
  56. ‘pid’=>3,     
  57. ‘name’=>‘小说’     
  58. ),     
  59. array(     
  60. ‘id’=>12,     
  61. ‘pid’=>9,     
  62. ‘name’=>‘铃声’     
  63. ),     
  64. array(     
  65. ‘id’=>13,     
  66. ‘pid’=>9,     
  67. ‘name’=>‘流行音乐’     
  68. ),     
  69. array(     
  70. ‘id’=>14,     
  71. ‘pid’=>9,     
  72. ‘name’=>‘古典音乐’     
  73. ),     
  74. array(     
  75. ‘id’=>15,     
  76. ‘pid’=>12,     
  77. ‘name’=>‘热门铃声’     
  78. ),     
  79. array(     
  80. ‘id’=>16,     
  81. ‘pid’=>12,     
  82. ‘name’=>‘搞笑铃声’     
  83. ),     
  84. array(     
  85. ‘id’=>17,     
  86. ‘pid’=>12,     
  87. ‘name’=>‘MP3铃声’     
  88. ),     
  89. array(     
  90. ‘id’=>18,     
  91. ‘pid’=>17,     
  92. ‘name’=>‘128K’     
  93. ),     
  94. array(     
  95. ‘id’=>19,     
  96. ‘pid’=>8,     
  97. ‘name’=>‘娱乐新闻’     
  98. ),     
  99. array(     
  100. ‘id’=>20,     
  101. ‘pid’=>11,     
  102. ‘name’=>‘穿越类’     
  103. ),     
  104. array(     
  105. ‘id’=>21,     
  106. ‘pid’=>11,     
  107. ‘name’=>‘武侠类’     
  108. ),     
  109. );     
  110. ?> 

无限分类函数,代码如下:

  1. <?php     
  2.           
  3. /**     
  4.           
  5. * Tree 树型类(无限分类)     
  6.          
  7.  
  8.           
  9. * @version 1.0     
  10.           
  11. * @access public      
  12. * @example      
  13. *   $tree= new Tree($result);     
  14.           
  15. *   $arr=$tree->leaf(0);     
  16.           
  17. *   $nav=$tree->navi(15);     
  18.           
  19. */ 
  20.           
  21. class Tree {     
  22.           
  23.         private $result;     
  24.           
  25.         private $tmp;     
  26.           
  27.         private $arr;     
  28.           
  29.         private $already = array();     
  30.           
  31.         /**     
  32.           
  33.          * 构造函数     
  34.           
  35.          *      
  36.          * @param array $result 树型数据表结果集     
  37.           
  38.          * @param array $fields 树型数据表字段,array(分类id,父id)     
  39.           
  40.          * @param integer $root 顶级分类的父id     
  41.           
  42.          */ 
  43.           
  44.         public function __construct($result$fields = array('id''pid'), $root = 0) {     
  45.           
  46.                 $this->result = $result;     
  47.           
  48.                 $this->fields = $fields;     
  49.           
  50.                 $this->root = $root;     
  51.           
  52.                 $this->handler();     
  53.           
  54.         }     
  55.           
  56.         /**     
  57.           
  58.          * 树型数据表结果集处理     
  59.           
  60.          */ 
  61.           
  62.         private function handler() {     
  63.           
  64.                 foreach ($this->result as $node) {     
  65.           
  66.                         $tmp[$node[$this->fields[1]]][] = $node;     
  67.           
  68.                 }     
  69.           
  70.                 krsort($tmp);     
  71.           
  72.                 for ($i = count($tmp); $i > 0; $i--) {     
  73.           
  74.                         foreach ($tmp as $k => $v) {     
  75.           
  76.                                 if (!in_array($k$this->already)) {     
  77.           
  78.                                         if (!$this->tmp) {     
  79.           
  80.                                                 $this->tmp = array($k$v);    
  81.           
  82.                                                 $this->already[] = $k;     
  83.           
  84.                                                 continue;     
  85.           
  86.                                         } else {     
  87.           
  88.                                                 foreach ($v as $key => $value) {     
  89.           
  90.                                                         if ($value[$this->fields[0]] == $this->tmp[0]) {     
  91.           
  92.                                                                 $tmp[$k][$key]['child'] = $this->tmp[1];     
  93.           
  94.                                                                 $this->tmp = array($k$tmp[$k]);     
  95.           
  96.                                                         }     
  97.           
  98.                                                 }     
  99.           
  100.                                         }     
  101.           
  102.                                 }     
  103.           
  104.                         }     
  105.           
  106.                         $this->tmp = null;     
  107.           
  108.                 }     
  109.           
  110.                 $this->tmp = $tmp;     
  111.           
  112.         }     
  113.           
  114.         /**     
  115.           
  116.          * 反向递归     
  117.           
  118.          */ 
  119.           
  120.         private function recur_n($arr$id) {     
  121.           
  122.                 foreach ($arr as $v) {     
  123.           
  124.                         if ($v[$this->fields[0]] == $id) {     
  125.           
  126.                                 $this->arr[] = $v;     
  127.           
  128.                                 if ($v[$this->fields[1]] != $this->root) $this->recur_n($arr$v[$this->fields[1]]);     
  129.           
  130.                         }     
  131.           
  132.                 }     
  133.           
  134.         }     
  135.           
  136.         /**     
  137.           
  138.          * 正向递归     
  139.           
  140.          */ 
  141.           
  142.         private function recur_p($arr) {     
  143.           
  144.                 foreach ($arr as $v) {     
  145.           
  146.                         $this->arr[] = $v[$this->fields[0]];     
  147.           
  148.                         if ($v['child']) $this->recur_p($v['child']);     
  149.           
  150.                 }     
  151.           
  152.         }     
  153.           
  154.         /**     
  155.           
  156.          * 菜单 多维数组     
  157.           
  158.          *      
  159.          * @param integer $id 分类id     
  160.           
  161.          * @return array 返回分支,默认返回整个树     
  162.           
  163.          */ 
  164.           
  165.         public function leaf($id = null) {     
  166.           
  167.                 $id = ($id == null) ? $this->root : $id;     
  168.           
  169.                 return $this->tmp[$id];     
  170.           
  171.         }     
  172.           
  173.         /**     
  174.           
  175.          * 导航 一维数组     
  176.           
  177.          *      
  178.          * @param integer $id 分类id     
  179.           
  180.          * @return array 返回单线分类直到顶级分类     
  181.           
  182.          */ 
  183.           
  184.         public function navi($id) {     
  185.           
  186.                 $this->arr = null;     
  187.           
  188.                 $this->recur_n($this->result, $id);     
  189.           
  190.                 krsort($this->arr);     
  191.           
  192.                 return $this->arr;     
  193.           
  194.         }     
  195.           
  196.         /**     
  197.           
  198.          * 散落 一维数组     
  199.           
  200.          *      
  201.          * @param integer $id 分类id     
  202.           
  203.          * @return array 返回leaf下所有分类id     
  204.           
  205.          */ 
  206.           
  207.         public function leafid($id) {     
  208.           
  209.                 $this->arr = null;     
  210.           
  211.                 $this->arr[] = $id;     
  212.           
  213.                 $this->recur_p($this->leaf($id));     
  214.           
  215.                 return $this->arr;     
  216.           
  217.         }     
  218.           
  219. }     
  220.           
  221. ?> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论