如何从数据库表创建列表视图?

I'm querying a category table id, parent_id, name to generate list view of all the categories and sub-categories. What I have so far is:

  • 生命值
  • 手提电脑
  • 个人电脑
  • 体育
  • 墨镜
  • 餐饮

我用来生成输出的函数是:

public function get_category($parent_id = NULL)
{ 
    $query = $this->db->get_where('category', array('parent_id' => $parent_id))->result();
    $result = '';

    foreach ($query as $row)
    {
        $i = 0;

        if ($i == 0) 
        {
            $result .= '<ul>';  
        }

        $result .= '<li><a href="'.base_url().'category/edit/'.$row->id.'">' . $row->name;
        $result .= $this->get_category($row->id);
        $result .= '</li>';

        $i++;

        if ($i > 0) 
        {
            $result .= '</ul>';
        }
    }

    return $result;
}

这是我要实现的目标:

HP
HP > Laptops
HP > PC
Sports
Sports > Sunglasses
Food
House