我正在使用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();
答案 0 :(得分:0)
我猜你在这里做了拼写错误。
请尝试以下代码:
$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();
答案 1 :(得分:0)
我找到了解决方案,现在是:
//select posts,cats,tags
$this->db->select("posts.*, GROUP_CONCAT(DISTINCT categories.cat_slug,'-',categories.cat_name) as cat",FALSE);
$this->db->from('posts');
//join cats
$this->db->join('post_cat', 'post_cat.post_id = posts.post_id', 'LEFT');
$this->db->join('categories', 'categories.cat_id = post_cat.cat_id', 'LEFT');
//get published blog
$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();