當前位置:首頁 > 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下級樹

分享到:

天气网首页彩吧