我想使用Codeigniter并列列出类别和子类别。就像这里:http://prntscr.com/mtj2ov
for example:
Flowers
Flowers -> Rose
Flowers -> Tulips
Flowers -> Lilies
id - subid - category_name - category_description - status
1 0 Flowers - 1
2 1 Rose - 1
3 1 Tulips - 1
public function getCategoryTree($id=0, $sub_mark=''){
$rows = $this->db->select('*')->where('subid', $id)->order_by('id','asc')->get('ci_category')->result();
$category = '';
if (count($rows) > 0) {
foreach ($rows as $row) {
$category .= '<option value="'.$row->id.'">'.$sub_mark.$row->category_name.'</option>';
$category .= $this->getCategoryTree($row->id, $sub_mark.'--');
}
}else{
return false;
}
return $category;
}
$data['all_categroy'] = $this->category_model->get_all_category();
与示例一样,我需要如何更改模型和控制文件
答案 0 :(得分:2)
尝试一下,
$result = $this->db->select('*')->order_by('id','asc')->get('ci_category')->result();
$parentName = '';
$select = "<select>";
foreach ($result as $r) {
if ($r->subid == 0) {
$select.= "<option id='" . $r->id . "' >" . $r->category_name . "</option>";
$parentName = $r->c_name . ' --> ';
} else {
$select.= "<option id='" . $r->id . "' >" . $parentName . $r->category_name . "</option>";
}
}
$select.= "</select>";
echo $select;
die;
答案 1 :(得分:-1)