当前位置:首页 > PHP教程 > php高级应用 > 列表

示例php+mysql查询实现无限下级分类树输出

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-26 12:12:03 浏览: 评论:0 

本文实例讲述了php+mysql查询实现无限下级分类树输出。分享给大家供大家参考,具体如下:

这里介绍的php结合mysql查询无限下级树输出,其实就是无限分类,给各位整理了几个php无限分类的例子.

树输出:

  1. function get_array($user_id,$top=0){ 
  2.  
  3. global $mysql,$_G; 
  4.  
  5.   $sql = "select user_id as name from `{spreads_users}`  where p1.spreads_userid='{$user_id}'"; 
  6.  
  7. $rows= $mysql->db_fetch_arrays($sql); 
  8.  
  9.   if($top==1){ 
  10.  
  11.   $arr[0]['name']=$user_id; 
  12.  
  13.   $arr[0]['children']=array(); 
  14.  
  15.   } 
  16.  
  17.   $top=$top+1; 
  18.  
  19. foreach ($rows as $key=>$value) 
  20.  
  21.  { 
  22.  
  23.      $r = get_array($value['name']); //调用函数,传入参数,继续查询下级 
  24.  
  25.      $arr[0]['children'][$key]['name']= $value['username']; //组合数组 
  26.  
  27.       if(is_array($r)){ 
  28.  
  29.       $arr[0]['children'][$key]['children']= $r[0]['children']; 
  30.  
  31.       } 
  32.  
  33.       $i++; 
  34.  
  35.     } 
  36.  
  37.     return $arr; 
  38.  
  39.   } 
  40.  
  41. $list = get_array("1000",1); //调用函数1000是顶级ID 
  42.  
  43. echo 'var data='.json_encode($list); 

这个是输出 Array 然后转让为 json

例子:

表结构:id字段为分类标识,name字段为分类名,father_id字段为所属父分类的id,path字段为分类路径,储存该分类祖先的集合,isdir判断是否是目录,1为是,0为否.

显示函数:

  1. //$count为分类等级 
  2.  
  3. sort_list($str,$fatherid,$count) 
  4.  
  5. { 
  6.  
  7. $rs = $this->sql->re_datas("select * from sort where father_id = fatherid"); 
  8.  
  9. $num = $this->sql->sql_numrows(); 
  10.  
  11. $i=0; 
  12.  
  13. $n = 1; 
  14.  
  15. while(isset($rs[$i])) 
  16.  
  17. { 
  18.  
  19. $name = ""; 
  20.  
  21. for($n = 1 ; $n < $count ; $n ) 
  22.  
  23. { 
  24.  
  25. $name.="│ "; 
  26.  
  27. } 
  28.  
  29. if($i 1==$num) 
  30.  
  31. { 
  32.  
  33. $name.="└─".$rs[$i][name]; 
  34.  
  35. } 
  36.  
  37. else 
  38.  
  39. { 
  40.  
  41. $name.="├─".$rs[$i][name]; 
  42.  
  43. } 
  44.  
  45. if($rs[$i][isdir]) 
  46.  
  47. { 
  48.  
  49. $str.="<span style='color:#CCCCCC'>".$name."</span>"; 
  50.  
  51. } 
  52.  
  53. else 
  54.  
  55. { 
  56.  
  57. $str.=$name"; 
  58.  
  59. } 
  60.  
  61. $temp = $count 1; 
  62.  
  63. $str = $this->sort_list($str,$rs[$i][id],$temp); 
  64.  
  65. $i ; 
  66.  
  67. } 
  68.  
  69. return $str; 
  70.  
  71. } 

其中$this->sql对象为sql操作类对象,re_datas()函数返回查到的数组,sql_numrows()函数返回查询到的数目.

调用方法:

$sort_list = sort_list($sort_list,0,1);

例子:

表:category

id int 主键,自增

name varchar 分类名称

pid int 父类id,默认0

顶级分类的 pid 默认就是0了,当我们想取出某个分类的子分类树的时候,基本思路就是递归,当然,出于效率问题不建议每次递归都查询数据库,通常的做法是先讲所有分类取出来,保存到PHP数组里,再进行处理,最后还可以将结果缓存起来以提高下次请求的效率.

先来构建一个原始数组,这个直接从数据库中拉出来就行:

  1. $categories = array( 
  2.  
  3.   array('id'=>1,'name'=>'电脑','pid'=>0), 
  4.  
  5.   array('id'=>2,'name'=>'手机','pid'=>0), 
  6.  
  7.   array('id'=>3,'name'=>'笔记本','pid'=>1), 
  8.  
  9.   array('id'=>4,'name'=>'台式机','pid'=>1), 
  10.  
  11.   array('id'=>5,'name'=>'智能机','pid'=>2), 
  12.  
  13.   array('id'=>6,'name'=>'功能机','pid'=>2), 
  14.  
  15.   array('id'=>7,'name'=>'超级本','pid'=>3), 
  16.  
  17.   array('id'=>8,'name'=>'游戏本','pid'=>3), 
  18.  
  19. ); 

目标是将它转化为下面这种结构:

  1. 电脑 
  2.  
  3. —笔记本 
  4.  
  5. ——-超级本 
  6.  
  7. ——-游戏本 
  8.  
  9. —台式机 
  10.  
  11. 手机 
  12.  
  13. —智能机 
  14.  
  15. —功能机 

用数组来表示的话,可以增加一个 children 键来存储它的子分类:

  1. array( 
  2.  
  3.   //1对应id,方便直接读取 
  4.  
  5.   1 => array( 
  6.  
  7.     'id'=>1, 
  8.  
  9.     'name'=>'电脑', 
  10.  
  11.     'pid'=>0, 
  12.  
  13.     children=>array( 
  14.  
  15.       &array( 
  16.  
  17.         'id'=>3, 
  18.  
  19.         'name'=>'笔记本', 
  20.  
  21.         'pid'=>1, 
  22.  
  23.         'children'=>array( 
  24.  
  25.           //此处省略 
  26.  
  27.         ) 
  28.  
  29.       ), 
  30.  
  31.       &array( 
  32.  
  33.         'id'=>4, 
  34.  
  35.         'name'=>'台式机', 
  36.  
  37.         'pid'=>1, 
  38.  
  39.         'children'=>array( 
  40.  
  41.           //此处省略 
  42.  
  43.         ) 
  44.  
  45.       ), 
  46.  
  47.     ) 
  48.  
  49.   ), 
  50.  
  51.   //其他分类省略 
  52.  
  53. ) 

处理过程:

  1. $tree = array(); 
  2.  
  3. //第一步,将分类id作为数组key,并创建children单元 
  4.  
  5. foreach($categories as $category){ 
  6.  
  7.   $tree[$category['id']] = $category; 
  8.  
  9.   $tree[$category['id']]['children'] = array(); 
  10.  
  11. } 
  12.  
  13. //第二部,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。 
  14.  
  15. foreach ($tree as $k=>$item) { 
  16.  
  17.   if ($item['pid'] != 0) { 
  18.  
  19.     $tree[$item['pid']]['children'][] = &$tree[$k]; 
  20.  
  21.   } 
  22.  
  23. } 

print_r($tree);打印结果如下:

  1. Array 
  2.  
  3. ( 
  4.  
  5.   [1] => Array 
  6.  
  7.     ( 
  8.  
  9.       [id] => 1 
  10.  
  11.       [name] => 电脑 
  12.  
  13.       [pid] => 0 
  14.  
  15.       [children] => Array 
  16.  
  17.         ( 
  18.  
  19.           [0] => Array 
  20.  
  21.             ( 
  22.  
  23.               [id] => 3 
  24.  
  25.               [name] => 笔记本 
  26.  
  27.               [pid] => 1 
  28.  
  29.               [children] => Array 
  30.  
  31.                 ( 
  32.  
  33.                   [0] => Array 
  34.  
  35.                     ( 
  36.  
  37.                       [id] => 7 
  38.  
  39.                       [name] => 超级本 
  40.  
  41.                       [pid] => 3 
  42.  
  43.                       [children] => Array 
  44.  
  45.                         ( 
  46.  
  47.                         ) 
  48.  
  49.                     ) 
  50.  
  51.                   [1] => Array 
  52.  
  53.                     ( 
  54.  
  55.                       [id] => 8 
  56.  
  57.                       [name] => 游戏本 
  58.  
  59.                       [pid] => 3 
  60.  
  61.                       [children] => Array 
  62.  
  63.                         ( 
  64.  
  65.                         ) 
  66.  
  67.                     ) 
  68.  
  69.                 ) 
  70.  
  71.             ) 
  72.  
  73.           [1] => Array 
  74.  
  75.             ( 
  76.  
  77.               [id] => 4 
  78.  
  79.               [name] => 台式机 
  80.  
  81.               [pid] => 1 
  82.  
  83.               [children] => Array 
  84.  
  85.                 ( 
  86.  
  87.                 ) 
  88.  
  89.             ) 
  90.  
  91.         ) 
  92.  
  93.     ) 
  94.  
  95.   [2] => Array 
  96.  
  97.     ( 
  98.  
  99.       [id] => 2 
  100.  
  101.       [name] => 手机 
  102.  
  103.       [pid] => 0 
  104.  
  105.       [children] => Array 
  106.  
  107.         ( 
  108.  
  109.           [0] => Array 
  110.  
  111.             ( 
  112.  
  113.               [id] => 5 
  114.  
  115.               [name] => 智能机 
  116.  
  117.               [pid] => 2 
  118.  
  119.               [children] => Array 
  120.  
  121.                 ( 
  122.  
  123.                 ) 
  124.  
  125.             ) 
  126.  
  127.           [1] => Array 
  128.  
  129.             ( 
  130.  
  131.               [id] => 6 
  132.  
  133.               [name] => 功能机 
  134.  
  135.               [pid] => 2 
  136.  
  137.               [children] => Array 
  138.  
  139.                 ( 
  140.  
  141.                 ) 
  142.  
  143.             ) 
  144.  
  145.         ) 
  146.  
  147.     ) 
  148.  
  149.   [3] => Array 
  150.  
  151.     ( 
  152.  
  153.       [id] => 3 
  154.  
  155.       [name] => 笔记本 
  156.  
  157.       [pid] => 1 
  158.  
  159.       [children] => Array 
  160.  
  161.         ( 
  162.  
  163.           [0] => Array 
  164.  
  165.             ( 
  166.  
  167.               [id] => 7 
  168.  
  169.               [name] => 超级本 
  170.  
  171.               [pid] => 3 
  172.  
  173.               [children] => Array 
  174.  
  175.                 ( 
  176.  
  177.                 ) 
  178.  
  179.             ) 
  180.  
  181.           [1] => Array 
  182.  
  183.             ( 
  184.  
  185.               [id] => 8 
  186.  
  187.               [name] => 游戏本 
  188.  
  189.               [pid] => 3 
  190.  
  191.               [children] => Array 
  192.  
  193.                 ( 
  194.  
  195.                 ) 
  196.  
  197.             ) 
  198.  
  199.         ) 
  200.  
  201.     ) 
  202.  
  203.   [4] => Array 
  204.  
  205.     ( 
  206.  
  207.       [id] => 4 
  208.  
  209.       [name] => 台式机 
  210.  
  211.       [pid] => 1 
  212.  
  213.       [children] => Array 
  214.  
  215.         ( 
  216.  
  217.         ) 
  218.  
  219.     ) 
  220.  
  221.   [5] => Array 
  222.  
  223.     ( 
  224.  
  225.       [id] => 5 
  226.  
  227.       [name] => 智能机 
  228.  
  229.       [pid] => 2 
  230.  
  231.       [children] => Array 
  232.  
  233.         ( 
  234.  
  235.         ) 
  236.  
  237.     ) 
  238.  
  239.   [6] => Array 
  240.  
  241.     ( 
  242.  
  243.       [id] => 6 
  244.  
  245.       [name] => 功能机 
  246.  
  247.       [pid] => 2 
  248.  
  249.       [children] => Array 
  250.  
  251.         ( 
  252.  
  253.         ) 
  254.  
  255.     ) 
  256.  
  257.   [7] => Array 
  258.  
  259.     ( 
  260.  
  261.       [id] => 7 
  262.  
  263.       [name] => 超级本 
  264.  
  265.       [pid] => 3 
  266.  
  267.       [children] => Array 
  268.  
  269.         ( 
  270.  
  271.         ) 
  272.  
  273.     ) 
  274.  
  275.   [8] => Array 
  276.  
  277.     ( 
  278.  
  279.       [id] => 8 
  280.  
  281.       [name] => 游戏本 
  282.  
  283.       [pid] => 3 
  284.  
  285.       [children] => Array 
  286.  
  287.         ( 
  288.  
  289.         ) 
  290.  
  291.     ) 
  292.  
  293. ) 

优点:关系清楚,修改上下级关系简单.

缺点:使用PHP处理,如果分类数量庞大,效率也会降低.

Tags: php+mysql无限下级分类树

分享到: