Php 如何在Codeigniter中编写类别/子类别数据库查询(一级深度)?

Php 如何在Codeigniter中编写类别/子类别数据库查询(一级深度)?,php,mysql,codeigniter,recursion,tree,Php,Mysql,Codeigniter,Recursion,Tree,啊,PHP中关于父/子类别的老问题。如果已经有人问过这个问题,我深表歉意(我知道它有多种形式),但是使用一个查询和Codeigniter进行搜索并不能具体回答如何做到这一点 基本上,我正在开发一个分类网站使用Codeigniter。可以将列表分配给一个类别。一个类别可以是独立的(没有子类别),或者一个类别可以有一个子类别 使用Codeigniter的“活动记录”功能,我有以下几点几乎可以工作 $this->db->select('l.*'); $this->db->sel

啊,PHP中关于父/子类别的老问题。如果已经有人问过这个问题,我深表歉意(我知道它有多种形式),但是使用一个查询和Codeigniter进行搜索并不能具体回答如何做到这一点

基本上,我正在开发一个分类网站使用Codeigniter。可以将列表分配给一个类别。一个类别可以是独立的(没有子类别),或者一个类别可以有一个子类别

使用Codeigniter的“活动记录”功能,我有以下几点几乎可以工作

$this->db->select('l.*');
$this->db->select('c.cat_parent, c.cat_slug, c.cat_name, c.cat_description, c.cat_display');
$this->db->select('c2.cat_slug AS parent_cat_slug, c2.cat_name AS parent_cat_name, c2.cat_description AS parent_cat_description, c2.cat_display AS parent_cat_display');

$this->db->limit($limit, $offset);

$this->db->join('listing_status ls', 'ls.status_id = l.listing_status');
$this->db->join('categories c', 'c.cat_id = l.listing_category');
$this->db->join('categories c2', 'c2.cat_parent = c.cat_id');

return $this->db->get('listings l')->result();

我希望能够拉出一个列表,它是指定的类别,如果该列表所属的类别有父类别,那么也可以获取父类别的详细信息。我已经添加了两个连接,应该可以正常工作了,有什么我遗漏的吗?

经过一番尝试,我意识到我的第二个连接是错误的。这里是我的更新和工作代码,希望它能帮助别人

$this->db->select('l.*');
$this->db->select('c.cat_parent, c.cat_slug, c.cat_name, c.cat_description, c.cat_display');
$this->db->select('pc.cat_slug AS parent_cat_slug, pc.cat_name AS parent_cat_name, pc.cat_description AS parent_cat_description, pc.cat_display AS parent_cat_display');

$this->db->limit($limit, $offset);

$this->db->join('listing_status ls', 'ls.status_id = l.listing_status');
$this->db->join('categories c', 'c.cat_id = l.listing_category');
$this->db->join('categories pc', 'pc.cat_id = c.cat_parent');

return $this->db->get('listings l')->result();

在一番胡闹之后,我意识到我的第二次加入是错误的。这里是我的更新和工作代码,希望它能帮助别人

$this->db->select('l.*');
$this->db->select('c.cat_parent, c.cat_slug, c.cat_name, c.cat_description, c.cat_display');
$this->db->select('pc.cat_slug AS parent_cat_slug, pc.cat_name AS parent_cat_name, pc.cat_description AS parent_cat_description, pc.cat_display AS parent_cat_display');

$this->db->limit($limit, $offset);

$this->db->join('listing_status ls', 'ls.status_id = l.listing_status');
$this->db->join('categories c', 'c.cat_id = l.listing_category');
$this->db->join('categories pc', 'pc.cat_id = c.cat_parent');

return $this->db->get('listings l')->result();