簡體   English   中英

獲取所有類別的帖子

[英]Get all categories of posts

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

我找到了解決方案,這里是:

//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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM