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

php结合mysql查询无限下级树输出

发布:smiling 来源: PHP粉丝网  添加日期:2016-08-30 14:37:29 浏览: 评论:0 

php结合mysql查询无限下级树输出的例子其实就是无限分类了这里给各位整理了几个php无限分类的例子,希望对各位有所帮助.

树输出:

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

这个是输出 Array 然后转让为 json 本教程由岑溪网站开发提供,实测.

例子:

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

显示函数:

  1. //$count为分类等级   
  2. sort_list($str,$fatherid,$count)   
  3. {   
  4. $rs = $this->sql->re_datas("select * from sort where father_id = fatherid");   
  5. $num = $this->sql->sql_numrows();   
  6. $i=0;   
  7. $n = 1;   
  8. while(isset($rs[$i]))   
  9. {   
  10. $name = "";   
  11. for($n = 1 ; $n < $count ; $n )   
  12. {   
  13. $name.="│ ";   
  14. }   
  15. if($i 1==$num)   
  16. {   
  17. $name.="└─".$rs[$i][name];   
  18. }   
  19. else   
  20. {   
  21. $name.="├─".$rs[$i][name];   
  22. }   
  23. if($rs[$i][isdir])   
  24. {   
  25. $str.="<span style='color:#CCCCCC'>".$name."</span>";   
  26. }   
  27. else   
  28. {   
  29. $str.=$name";   
  30. }   
  31. $temp = $count 1;   
  32. $str = $this->sort_list($str,$rs[$i][id],$temp);   
  33. $i ;   
  34. }   
  35. return $str;   

其中$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.     array('id'=>1,'name'=>'电脑','pid'=>0), 
  3.     array('id'=>2,'name'=>'手机','pid'=>0), 
  4.     array('id'=>3,'name'=>'笔记本','pid'=>1), 
  5.     array('id'=>4,'name'=>'台式机','pid'=>1), 
  6.     array('id'=>5,'name'=>'智能机','pid'=>2), 
  7.     array('id'=>6,'name'=>'功能机','pid'=>2), 
  8.     array('id'=>7,'name'=>'超级本','pid'=>3), 
  9.     array('id'=>8,'name'=>'游戏本','pid'=>3), 
  10. ); 

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

  1. 电脑 
  2. —笔记本 
  3. ——-超级本 
  4. ——-游戏本 
  5. —台式机 
  6. 手机 
  7. —智能机 
  8. —功能机 

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

  1. array
  2.     //1对应id,方便直接读取 
  3.     1 => array
  4.         'id'=>1, 
  5.         'name'=>'电脑'
  6.         'pid'=>0, 
  7.         children=>array
  8.             &array
  9.                 'id'=>3, 
  10.                 'name'=>'笔记本'
  11.                 'pid'=>1, 
  12.                 'children'=>array
  13.                     //此处省略 
  14.                 ) 
  15.             ), 
  16.             &array
  17.                 'id'=>4, 
  18.                 'name'=>'台式机'
  19.                 'pid'=>1, 
  20.                 'children'=>array
  21.                     //此处省略 
  22.                 ) 
  23.             ), 
  24.         ) 
  25.     ), 
  26.     //其他分类省略 

处理过程:

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

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

  1. Array 
  2.     [1] => Array 
  3.         ( 
  4.             [id] => 1 
  5.             [name] => 电脑 
  6.             [pid] => 0 
  7.             [children] => Array 
  8.                 ( 
  9.                     [0] => Array 
  10.                         ( 
  11.                             [id] => 3 
  12.                             [name] => 笔记本 
  13.                             [pid] => 1 
  14.                             [children] => Array 
  15.                                 ( 
  16.                                     [0] => Array 
  17.                                         ( 
  18.                                             [id] => 7 
  19.                                             [name] => 超级本 
  20.                                             [pid] => 3 
  21.                                             [children] => Array 
  22.                                                 ( 
  23.                                                 ) 
  24.                                         ) 
  25.                                     [1] => Array 
  26.                                         ( 
  27.                                             [id] => 8 
  28.                                             [name] => 游戏本 
  29.                                             [pid] => 3 
  30.                                             [children] => Array 
  31.                                                 ( 
  32.                                                 ) 
  33.                                         ) 
  34.                                 ) 
  35.                         ) 
  36.                     [1] => Array 
  37.                         ( 
  38.                             [id] => 4 
  39.                             [name] => 台式机 
  40.                             [pid] => 1 
  41.                             [children] => Array 
  42.                                 ( 
  43.                                 ) 
  44.                         ) 
  45.                 ) 
  46.         ) 
  47.     [2] => Array 
  48.         ( 
  49.             [id] => 2 
  50.             [name] => 手机 
  51.             [pid] => 0 
  52.             [children] => Array 
  53.                 ( 
  54.                     [0] => Array 
  55.                         ( 
  56.                             [id] => 5 
  57.                             [name] => 智能机 
  58.                             [pid] => 2 
  59.                             [children] => Array 
  60.                                 ( 
  61.                                 ) 
  62.                         ) 
  63.                     [1] => Array 
  64.                         ( 
  65.                             [id] => 6 
  66.                             [name] => 功能机 
  67.                             [pid] => 2 
  68.                             [children] => Array 
  69.                                 ( 
  70.                                 ) 
  71.                         ) 
  72.                 ) 
  73.         ) 
  74.     [3] => Array 
  75.         ( 
  76.             [id] => 3 
  77.             [name] => 笔记本 
  78.             [pid] => 1 
  79.             [children] => Array 
  80.                 ( 
  81.                     [0] => Array 
  82.                         ( 
  83.                             [id] => 7 
  84.                             [name] => 超级本 
  85.                             [pid] => 3 
  86.                             [children] => Array 
  87.                                 ( 
  88.                                 ) 
  89.                         ) 
  90.                     [1] => Array 
  91.                         ( 
  92.                             [id] => 8 
  93.                             [name] => 游戏本 
  94.                             [pid] => 3 
  95.                             [children] => Array 
  96.                                 ( 
  97.                                 ) 
  98.                         ) 
  99.                 ) 
  100.         ) 
  101.     [4] => Array 
  102.         ( 
  103.             [id] => 4 
  104.             [name] => 台式机 
  105.             [pid] => 1 
  106.             [children] => Array 
  107.                 ( 
  108.                 ) 
  109.         ) 
  110.     [5] => Array 
  111.         ( 
  112.             [id] => 5 
  113.             [name] => 智能机 
  114.             [pid] => 2 
  115.             [children] => Array 
  116.                 ( 
  117.                 ) 
  118.         ) 
  119.     [6] => Array 
  120.         ( 
  121.             [id] => 6 
  122.             [name] => 功能机 
  123.             [pid] => 2 
  124.             [children] => Array 
  125.                 ( 
  126.                 ) 
  127.         ) 
  128.     [7] => Array 
  129.         ( 
  130.             [id] => 7 
  131.             [name] => 超级本 
  132.             [pid] => 3 
  133.             [children] => Array 
  134.                 ( 
  135.                 ) 
  136.         ) 
  137.     [8] => Array 
  138.         ( 
  139.             [id] => 8 
  140.             [name] => 游戏本 
  141.             [pid] => 3 
  142.             [children] => Array 
  143.                 ( 
  144.                 ) 
  145.         ) 

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

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

Tags: mysql查询 php下级树

分享到: