2012-07-25 63 views
0

我有如下表:檢索分層數據從表中UL L1標籤顯示樹狀結構

id  name      lft  rgt  level 
---  ------------------------- ---- ---- ----- 
1  company name    1  16  0 
2  HR       2  3  1 
3  Superwiser     4  9  1 
4  Associates     5  6  2 
5  test      10  13  1 
6  test2      11  12  2 

使用這個數據庫,我想顯示UL標籤裏的樹結構。但沒有得到這張桌子。我要顯示這樣的:

1. Company Name 
|--:Hr 
|--:Superwiser 
    |--:Associates 
|--:test 
    |--:test2 

我怎麼能火,這和如何UL L1標籤來顯示它特定的查詢。提前致謝。

+0

你能解釋一下你的方案給我嗎?通常在樹結構中,通常只有一個「ParentID」列將該行鏈接到它所屬的另一行。如果你有這個,根據你的輸出沒有多大意義 – Gavin 2012-07-25 08:34:48

回答

1
<?php 

類分類01​​{

var $table=''; 
    var $CI =''; 
    var $ul_class=''; 

    function Category($config=array()){ 
     $this->table=$config['table']; 
     $this->CI=& get_instance(); 
     $this->CI->load->database(); 
     $this->ul_class=$config['ul_class']; 
    } 
    function getTree($parent_id=0){ 

     $this->CI->db->where('parent_id',$parent_id); 
     $first_level=$this->CI->db->get('category')->result(); 
     $tree= '<ul class="'.$this->ul_class.'">'; 
     foreach($first_level as $fl){ 
      $tree.='<li>'.$fl->name; 
      $this->CI->db->where('parent_id',$fl->cat_id); 

      $count=$this->CI->db->count_all_results($this->table); 

      if($count!=0){ 
       $tree.=$this->getTree($fl->cat_id); 
      } 
      $tree.= '</li>'; 
     } 
     $tree.= '</ul>'; 

     return $tree; 
    } 

} ?>

使用這個庫

+0

你能否澄清上面的代碼,你在這裏做了什麼。 – Sky 2012-07-25 06:54:09

+0

兄弟你的數據庫結構是錯誤的..應該有一個父域ID的字段。我將向你展示一個例子。 – 2012-07-27 06:11:55

0

遞歸調用數據庫是魔鬼的跡象,所以我的方式通常處理這,它自己使用PHP。

function buildTree($ParentID, $Categories, $Output = '') 
{ 
    foreach($Categories as $Category) 
    { 
     // Skip any categories that are not 
     // in the current parent. 
     if($Category->ParentID != $ParentID) 
      continue; 
     // Add the current category to the output 
     $Output .= '<li>' . $Category->name . '</li>'; 
     // If the category has children, recurse another level. 
     if($Category->ChildCount > 0) 
     { 
      $Output .= '<ul>'; 
      $Output .= $this->buildTree($Category->ID, $Categories, $Output); 
      $Output .= '</ul>'; 
     } 
    } 
    return $Output; 
} 

,那麼只需在調用它:

<ul><?= buildTree(0, $Categories); ?></ul> 

的代碼依賴於你做一個子查詢,返回屬於該父行子行的計。

select *, (select count(C2.*) from Category C2 where C2.ParentID = C1.ID) as ChildCount from Category as C1 

這就可以讓你只能繼續遞歸如果父節點實際上有孩子,防止空UL的被添加到輸出的時候沒有孩子存在。

1

CREATE TABLE IF NOT EXISTS categorycat_id INT(11)NOT NULL AUTO_INCREMENT, name VARCHAR(255)COLLATE utf8_bin DEFAULT NULL, image VARCHAR(255)COLLATE utf8_bin DEFAULT NULL, parent_id INT(11)NOT NULL DEFAULT '0', top TINYINT(1)NOT NULL, column INT(3)NOT NULL, sort_order INT(3)NOT NULL DEFAULT '0', status TINYINT(1)NOT NULL, total_product INT( 11)NOT NULL, date_added INT(11)NOT NULL, date_modified INT(11)NOT NULL, PRIMARY KEY(cat_id) )ENGINE = MyISAM的默認字符集= UTF8 COLLATE = utf8_bin AUTO_INCREMENT = 17;

這是我的數據庫表,並即時將與此表 創建樹這裏是我的功能

function getTree(){ 
$config['ul_class']='tree'; 
$config['table']='category'; 
$this->load->library('category',$config); 
echo $this->category->getTree(); 
} 

和圖書館一樣

<?php 

類分類01​​{

var $table=''; 
    var $CI =''; 
    var $ul_class=''; 

    function Category($config=array()){ 
     $this->table=$config['table']; 
     $this->CI=& get_instance(); 
     $this->CI->load->database(); 
     $this->ul_class=$config['ul_class']; 
    } 
    function getTree($parent_id=0){ 

     $this->CI->db->where('parent_id',$parent_id); 
     $first_level=$this->CI->db->get('category')->result(); 
     $tree= '<ul class="'.$this->ul_class.'">'; 
     foreach($first_level as $fl){ 
      $tree.='<li>'.$fl->name; 
      $this->CI->db->where('parent_id',$fl->cat_id); 

      $count=$this->CI->db->count_all_results($this->table); 

      if($count!=0){ 
       $tree.=$this->getTree($fl->cat_id); 
      } 
      $tree.= '</li>'; 
     } 
     $tree.= '</ul>'; 

     return $tree; 
    } 

} ?>

你必須在應用程序文件夾保存此類文件的庫文件夾裏面有文件名類