使用CodeIgniter并排显示类别

时间:2019-03-05 13:09:27

标签: php codeigniter

我想使用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();

与示例一样,我需要如何更改模型和控制文件

2 个答案:

答案 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)

尝试生成SEO友好URL

遵循以下条件:

https://www.codexworld.com/generate-seo-friendly-url-codeigniter/