2012-01-24 18 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

この関数からの文字列として – 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; 
} 
?> 

今、階層内のすべてのカテゴリを表示するには、再帰関数を呼び出す:

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

詳細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(); 


?> 
関連する問題