2012-01-24 7 views
2

CodeIgniter는 단순히 MySQL의에서 다단계 범주 계층 구조를 만들방법 멀티 레벨 카테고리 계층 구조 (카테고리 트리) 만들기 -

분류 표 :

________________________________________________________________________ 
| id    | parent_id  | name 
———————————————————————————————————————————————————————————————————————— 
| 1    | 0    | Root 
| 2    | 1    | Sub category of root 
| 3    | 0    | category 1 
| 4    | 3    | sub category of category 1 
| 5    | 4    | sub category of first sub category of category 1 
———————————————————————————————————————————————————————————————————————— 

PHP

public function getCategoryTree($level = 0) { 
    $rows = $this->db 
      ->select(‘id,parent_id,name’) 
      ->where(‘parent_id’, $level) 
      ->get(‘categories’) 
      ->result(); 

    if (count($rows) > 0) { 
     foreach ($rows as $row) { 
      $rows = $this->getCategoryTree($row->id); 
     } 
    } 
    //return $rows; 
} 


echo $rows; 

// output will be show as string so i have to return this string in a variable 

Root 
—Sub category of root 
category 1 
—sub category of category 1 
——sub category of first sub category of category 1 
+0

가 어떻게 값을 반환 할 수

<?php echo $data['category'] = getCategories(0, $category);?> 

세부 사항을 이 함수의 문자열 – AZinkey

+1

카테고리 트리가 커지지 않는다면 모든 데이터를 가져 와서 PHP로 정렬 할 수 있습니다. – AmazingDreams

답변

4

코드에서 가장 큰 문제는 foreach 루프 내에 $rows을 덮어 쓰는 것이 었습니다.

또한 재귀 적 솔루션을 사용하는 경우처럼 내부 호출에서 반환 된 기능/메소드를 추적하는 것이 중요합니다.

또한 주문을 추가하여 루트 카테고리가 먼저 표시되는지 확인했습니다.

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

    $category = ''; 
    if (count($rows) > 0) { 
     foreach ($rows as $row) { 
      $category .= $prefix . $row->name . "\n"; 
      // Append subcategories 
      $category .= $this->getCategoryTree($row->id, $prefix . '-'); 
     } 
    } 
    return $category; 
} 

public function printCategoryTree() { 
    echo $this->getCategoryTree(); 
} 
+0

정말 고마워요. – AZinkey

0

보십시오 다음 ...

<?php 
$sql = "SELECT id, name, parent_id FROM category ORDER BY parent_id, id"; 
$results = mysqli_query($con,$sql) or die(mysqli_error()) ; 
if($results) 
{ 
    while($result = mysqli_fetch_array($results)) 
    { 
     $category['categories'][$result['id']] = $result; 
     $category['parent_cats'][$result['parent_id']][] = $result['id']; 
    } 
} 
?> 

<?php 
function getCategories($parent, $category) 
{ 
    $html = ""; 
    if (isset($category['parent_cats'][$parent])) 
    { 
     $html .= "<ul>\n"; 
     foreach ($category['parent_cats'][$parent] as $cat_id) 
     { 
      if (!isset($category['parent_cats'][$cat_id])) 
      { 
       $html .= "<li>".$category['categories'][$cat_id]['name']."</li> \n"; 
      } 
      if (isset($category['parent_cats'][$cat_id])) 
      { 
       $html .= "<li>". $category['categories'][$cat_id]['name'] . " \n"; 
       $html .= getCategories($cat_id, $category); 
       $html .= "</li> \n"; 
      } 
     } 
     $html .= "</ul> \n"; 
    } 
    return $html; 
} 
?> 

지금 모든 계층에서 카테고리 표시 재귀 함수를 호출 : info

0
<?php 

    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 


    $conn = new mysqli($servername, $username, $password,'test'); 


if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 




function getCategoryTree($level = 0, $prefix = '') { 


$category = ''; 
$sql = "SELECT * FROM category WHERE parent_id = $level ORDER BY id asc"; 
$result = $GLOBALS['conn']->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 

     $category .= $prefix . $row['name'] . "<br/>"; 
     $category .= getCategoryTree($row['id'], $prefix . '-'); 
    } 
} 
return $category; 
} 

function printCategoryTree() { 
    echo getCategoryTree(); 
} 

printCategoryTree(); 


?> 
관련 문제