我正在使用 Codeigniter。我想显示所有帖子及其各自的类别。我已经制作了一个代码,但它只显示帖子的一个类别。为什么我的代码没有获取帖子的所有类别?任何解决方案或改进都会很好。这是我的代码:
$this->db->select("posts.*, categories.*");
$this->db->from( 'posts' );
//get cats
$this->db->join('post_cat', 'posts.post_id = post_cat.post_id', 'LEFT');
$this->db->join('categories', 'post_cat.cat_id = post_cat.cat_id', 'LEFT');
//get published blogs
$this->db->where('post_type', 'blog');
$this->db->where('post_status', 'published');
$this->db->group_by("posts.post_id");
$this->db->order_by("post_id", "desc");
$query = $this->db->get();
return $query->result_array();
最佳答案
我猜你在这里打字错误。
尝试下面的代码:
$this->db->select("posts.*, categories.*");
$this->db->from( 'posts' );
//get cats
$this->db->join('post_cat', 'posts.post_id = post_cat.post_id', 'LEFT');
$this->db->join('categories', 'post_cat.cat_id = categories.id', 'LEFT');
// you did typo in above statement here ==> post_cat.cat_id = post_cat.cat_id
//get published blogs
$this->db->where('post_type', 'blog');
$this->db->where('post_status', 'published');
$this->db->group_by("posts.post_id");
$this->db->order_by("post_id", "desc");
$query = $this->db->get();
return $query->result_array();
关于php - 获取所有类别的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28302820/