2017-08-28 98 views
-1

This is the db structure笨樹視圖構建數據

[ 
    { 
    text: 'Parent 1', 
    href: '#parent1', 
    tags: ['4'], 
    nodes: [ 
     { 
     text: 'Child 1', 
     href: '#child1', 
     tags: ['2'], 
     nodes: [ 
      { 
      text: 'Grandchild 1', 
      href: '#grandchild1', 
      tags: ['0'] 
      }, 
      { 
      text: 'Grandchild 2', 
      href: '#grandchild2', 
      tags: ['0'] 
      } 
     ] 
     }, 
     { 
     text: 'Child 2', 
     href: '#child2', 
     tags: ['0'] 
     } 
    ] 
    }, 
    { 
    text: 'Parent 2', 
    href: '#parent2', 
    tags: ['0'] 
    }, 
    { 
    text: 'Parent 3', 
    href: '#parent3', 
    tags: ['0'] 
    } 
] 

我使用的代碼是這樣的,

public function getCategoryTree($level = 0, $prefix = '') { 
     $rows = $this->db 
      ->select('id,parent_id,name') 
      ->where('parent_id', $level) 
      ->order_by('id','asc') 
      ->get('category') 
      ->result(); 

     $json_response = array(); 
     foreach ($rows as $row) 
     { 
      $row_array = array(); 
      $row_array['text'] = $row->name;   
      $row_array['nodes'] = array(); 
      $newlevel = $row->id; 

     $childs = $this->db 
      ->select('id,parent_id,name') 
      ->where('parent_id', $newlevel) 
      ->order_by('id','asc') 
      ->get('category') 
      ->result(); 

      if(count($childs) > 0){ 
       foreach ($childs as $row) 
       { 
        $row_array['nodes'][] = array(
         'text' => $row->name, 
        ); 
       } 
      } 
      array_push($json_response, $row_array); //push the values in the array 
     } 
     return $json_response; 

    } 

輸出我得到: output

上面的代碼僅給出一個水平,但我想深入。如何格式化循環以完成它?

+0

不要把數據庫結構的圖像。 – aristotll

+0

我更新了我的答案,告訴你如何去做你所需要的。 –

回答

0

嘗試以下方法:

public function getCategoryTree() 
{ 
    $query = $this->db->select('id,parent_id,name') 
     ->order_by('parent_id','asc') 
     ->get('categories'); 

    if($query->num_rows() > 0) 
    { 
     $result = $query->result_array(); 

     foreach($result as $k => $v) 
     { 
      $count = 0; 
      $v['href'] = '#'. strtolower(str_replace(' ', '', $v['name'])); 

      foreach($result as $k2 => $v2) 
      { 
       // if child has this parent, count 
       if($v['id'] == $v2['parent_id']) 
        $count++; 
      } 

      if($count) 
       $v['tags'] = array((string)$count); 

      $mod[] = $v; 
     } 

     $tree = $this->buildTree($mod); 

     $this->key_unset_recursive($tree, 'id'); 
     $this->key_unset_recursive($tree, 'parent_id'); 

     echo json_encode($tree, JSON_PRETTY_PRINT); 
    } 

} 

public function buildTree(array $elements, $parentId = 0) 
{ 
    $branch = array(); 

    foreach($elements as $element) 
    { 
     if($element['parent_id'] == $parentId) 
     { 
      $children = $this->buildTree($elements, $element['id']); 

      if($children) 
      { 
       $element['nodes'] = $children; 
      } 

      $branch[] = $element; 
     } 
    } 

    return $branch; 
} 

public function key_unset_recursive(&$array, $remove) 
{ 
    foreach($array as $key => &$value) 
    { 
     if($key === $remove) 
     { 
      unset($array[$key]); 
     } 

     else if(is_array($value)) 
     { 
      $this->key_unset_recursive($value, $remove); 
     } 
    } 
}