php - 获取所有类别的帖子

标签 php mysql codeigniter activerecord

我正在使用 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/

相关文章:

mysql - 带变量的 SQL 更新语句

javascript - 自动提交不适用于此脚本

php - 如何从 Mysql 查询转换为 Codeigniter 语法?

php - 代码点火器在 WHERE 子句后附加 IS NULL

html - 使用 BBCode 输入和 HTML 输出避免 XSS

php - 未设置 HTML 组合框值

php - 如何从 laravel 5.5 的存储内部文件夹中获取文件?

mysql - 从谷歌云sql上的备份创建实例

php - 如何在 PHP 中用链接和主题标签替换纯文本 URL

php - 如何根据值查询相关实体