2015-02-11 117 views
5

我想使用codeigniter和mysql動態創建一個動態樹形菜單,我沒有任何問題,用於樹結構的jQuery的jQuery的&。如何使用codeigniter和mysql動態生成樹結構

我有兩個表,表組織&組織類:

(id, parent_org_id, org_class_id, title) 
(1,  0,    1,   Company Name) 
(2,  0,    1,   Company2 Name) 
(3,  1,    2,   Departement Name) 
(4,  2,    2,   Departement2 Name) 
(5,  4,    3,   Division2 Name) 

(id,  org_class_title, order_no) 
(1,  0,    1) 
(2,  0,    2) 
(3,  1,    3) 

這是我的函數:

$str =''; 
$lastListLevel=0; 
$firstRow=true; 

foreach($organization->result() as $row){ 
    $currentListLevel=$row->parent_organization_id -1 ; // minus one to correct level to Level 0 
    $differenceLevel=$currentListLevel-$lastListLevel; 

    $rootLevel=false; 

    if($differenceLevel==0){   
     if($firstRow){ 
      $firstRow=false; 
     }else{ 
      $str .='</li>'; 
     } 
     $str .='<li>'; 
    }elseif($differenceLevel>0){  
     for($j=0;$j<$differenceLevel;$j++){ 
      $str .='<ul><li>'; 
     } 
    }elseif($differenceLevel<0){  
     for($j=$differenceLevel;$j<=0;$j++){ 
      if($j==0){ // root level reached 
       $rootLevel=true; 
       $str .='</li>'; 
      }else{ 
       $str .='</li></ul>'; 
      } 
     } 
     $str .= ($rootLevel) ? '<li>' : '<ul>'; 
    } 
    $str.='<span><i class="icon-minus-sign"></i>'.$row->parent_organization_id.'-'.$row->organization_class_id.'-'.$row->id.'--'.$row->title.'--'.$row->organization_class.'</span>'.'<button type="button" class="btn btn-info btn-small" data-toggle="modal" data-target="#editorganizationModal'.$row->id.'"><i class="icon-paste"></i></button>';?> 

    <?php 
    $lastListLevel=$currentListLevel; 

}echo $str.'<br>Lowest organization level. Please define new lower level to add.<br />';?> 

這是我的模型:

function getOrganization(){ 
     $this->db->select('organization.*, organization.id as id, parent.id as parent_id, parent.title as organization_parent, organization_class.title as organization_class, organization.title as title'); 
     $this->db->from('organization'); 
     $this->db->join('organization_class', 'organization.organization_class_id = organization_class.id', 'left'); 
     $this->db->join('organization as parent', 'organization.parent_organization_id = parent.id', 'left'); 
     $this->db->order_by('organization.organization_class_id', 'asc'); 
     $query = $this->db->get(); 
     return $query; 
    } 

我正好可以彌補樹爲organization_parent,結果是這樣的:

-- Company Name - Company 
-- Company Name 2 - Company 
       --Departement Name - Departement 
       --Departement2 Name - Departement 
         --Division Name2 - Division 

,我想使結果是這樣的:

-- Company Name - Company 
    --Departement Name - Departement 

-- Company Name2 - Company 
    --Departement2 Name - Departement 
     --Division2 Name - Division 

或結果會是這樣,如果在HTML:

<ul> 
    <li>Company Name - Company 
     <ul> 
      <li>Departement Name - Departement</li> 
     </ul> 
    </li> 

    <li>Company Name2 - Company 
     <ul> 
      <li>Departement Name2 - Departement 
       <ul> 
        <li>Divison Name2 - Division</li> 
       </ul> 
      </li> 
     </ul> 
    </li> 
</ul> 
+0

您是否將數據放入與您的樹結構相關的數組結構中? – Edward 2015-02-11 09:18:10

+0

我已經添加了我的模型, – 2015-02-11 09:28:38

+0

作爲一般提示,在這類任務中,您必須定義一個創建樹節點的函數,並有條件地調用它自己以添加子節點。即函數dTree(){//代碼; if(條件){dTree();}}'該條件即將檢查節點是否有子節點。 – SaidbakR 2015-02-11 09:38:31

回答

5

請將看到$organization->result()是有幫助

讓我們假設你使用測試功能

public function test()//may be index() or something else.Rename this funciton name as yours 
{ 
    //other codes getting data from model 
    $results=$organization->result();//capture the query data inside $results variable 
    $menu=$this->get_menu($results,0); //get_menu() function is bellow 
    //$menu will contain your ul li structure 
    //send it to view or echo 
} 

public function get_menu($results,$parent_id) 
{ 
    $menu='<ul>'; 

    for($i=0;$i<sizeof($results);$i++) 
    { 
     if($results[$i]->parent_id==$parent_id) 
     { 
      if($this->has_child($results,$results[$i]->id)) 
      { 
       $sub_menu= $this->get_menu($results,$results[$i]->id); 
       $menu.='<li>'.$results[$i]->title.'-'.$results[$i]->organization_class.$sub_menu.'</li>'; 
      } 
      else 
      { 
       $menu.='<li>'.$results[$i]->title.'-'.$results[$i]->organization_class.'</li>'; 
      } 
     } 
    } 
    $menu.='</ul>'; 
    return $menu; 
} 
public function has_child($results,$id) 
{ 
    for($i=0;$i<sizeof($results);$i++) 
    { 
     if($results[$i]->parent_id==$id) 
     { 
      return true; 
     } 
    } 
    return false; 
} 

get_menu是遞歸函數,它將適用於任何子層
希望它能幫助你。

+0

謝謝shaiful伊斯蘭教,這是我尋找的東西,你真的幫我:) – 2015-02-12 00:13:00