我编写了一个模型函数来使用codeigniter获取类别及其子类别。我想在一个下拉列表中显示完整的层次结构。
Category
sub1
sub1.1
sub1.2
sub2
sub3
sub3.1
sub3.1.1
and so on.
MY model
public function getCategoryTreeForParentId($parent_id = 0) {
$categories = array();
$this->db->from('jil_category');
$this->db->where('ctg_parent', $parent_id);
$result = $this->db->get()->result();
foreach ($result as $mainCategory) {
$category = array();
$category['id'] = $mainCategory->ctg_id;
$category['name'] = $mainCategory->ctg_name;
$category['parent_id'] = $mainCategory->ctg_parent;
$category['sub_categories'] = $this->getCategoryTreeForParentId($category['id']);
$categories[$mainCategory->ctg_id] = $category;
}
return $categories;
}