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
この関数からの文字列として – AZinkey
あなたのカテゴリツリーが大きく成長しないことが分かっているならば、私はちょうどすべてのデータをフェッチしてPHPで整理したいと思います。 – AmazingDreams