Php 根据CodeIgniter中pivot表中的类别ID显示帖子

Php 根据CodeIgniter中pivot表中的类别ID显示帖子,php,codeigniter,pivot-table,codeigniter-3,codeigniter-query-builder,Php,Codeigniter,Pivot Table,Codeigniter 3,Codeigniter Query Builder,我目前有三张这样的表格: ci\u帖子:id、标题、slug、内容 ci\u术语:术语id、标题、slug ci关系:id、post\u id、term\u id 我正在尝试根据特定的点击类别检索所有帖子 这是我在模型中使用的方法,但我无法使其按我所希望的方式工作: public function get_posts_category($id){ $this->db->select('*'); $this->db->from($this->tabl

我目前有三张这样的表格:

ci\u帖子:id、标题、slug、内容

ci\u术语:术语id、标题、slug

ci关系:id、post\u id、term\u id

我正在尝试根据特定的点击类别检索所有帖子

这是我在模型中使用的方法,但我无法使其按我所希望的方式工作:

public function get_posts_category($id){

    $this->db->select('*');
    $this->db->from($this->table);
    $this->db->join('ci_relationship', 'ci_relationship.post_id = ci_posts.id', 'INNER');
    //$this->db->join('ci_users', 'ci_users.id = ci_relationship.id', 'INNER');
    //$this->db->join('ci_terms', 'ci_relationship.term_id = ci_terms.term_id', 'INNER');
    $this->db->order_by('post_id', 'DESC');
    $this->db->group_by('post_id');
    //$this->db->where('type', 'category');
    $this->db->where('term_id', $id);

    $query = $this->db->get();

    if($query->num_rows() >= 1){
        return $query->result();
    } else {
        return false;
    }

}
如$this->db->where('term_id',$id)上面的代码段所示应能够显示与单击的类别匹配的所有帖子,该类别存储在ci_关系表中,并在ci_术语表中创建,但即使ci_关系表中存在多个相互关联的ci_帖子和ci_术语,当前输出也不会显示任何内容

有人能解释一下我的错误在哪里吗

更新这是我访问它们的方式:

<?php if($categories) : ?>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h1 class="panel-title">Categories</h1>
    </div>
    <div class="panel-body">
      <?php foreach($categories as $cats) : ?>
        <a href="<?= base_url('posts/category/'.get_category_slug($cats->term_id)) ?>"><span class="label label-orange"><?= get_category($cats->term_id); ?></span></a>
      <?php endforeach; ?>
    </div>
  </div>
<?php endif; ?>
这是控制器中获取标签的方法,当标签存在于下面的ci_关系表中时:

// Get categories
public function get_categories(){

    $this->db->select('*');
    $this->db->from('ci_relationship');
    $this->db->group_by('term_id');
    $this->db->where('type', 'category');

    $query = $this->db->get();

    if($query->num_rows() >= 1){
        return $query->result();
    } else {
        return false;
    }

}
但是上面的代码只是为了显示,根据单击的类别显示帖子的实际功能仍然是get_posts_category方法


提前感谢

希望这对您有所帮助:

您的
get\u posts\u类别应如下所示:


替换
$this->db->where('term_id',$id)使用此
$this->db->where('ci\u relationship.term\u id',$id)它仍然没有显示任何内容…在
order\u by
group\u by
子句中也进行更改,与上面的注释相同,您的意思是这样编辑它们:
$this->db->order\u by('ci\u relationship.post\u id',DESC')
$this->db->group_by('ci_relationship.post_id')我仍然一无所获:/不工作:/。我必须说,我不是在传递id,而是在助手get_term_slug($term->term_id)的帮助下传递slug,这是一个问题吗?请参阅我的更新问题,它可能会让您更好地了解我是如何构建函数的,谢谢您的帮助
// Get categories
public function get_categories(){

    $this->db->select('*');
    $this->db->from('ci_relationship');
    $this->db->group_by('term_id');
    $this->db->where('type', 'category');

    $query = $this->db->get();

    if($query->num_rows() >= 1){
        return $query->result();
    } else {
        return false;
    }

}
public function get_posts_category($id)
{

    $this->db->select('*, count(ci_posts.id)');
    $this->db->from('ci_posts');
    $this->db->join('ci_relationship', 'ci_relationship.post_id = ci_posts.id');
    //$this->db->join('ci_users', 'ci_users.id = ci_relationship.id', 'INNER');
    //$this->db->join('ci_terms', 'ci_relationship.term_id = ci_terms.term_id', 'INNER');
    $this->db->order_by('ci_posts.id', 'DESC');
    $this->db->group_by('ci_posts.id');
    //$this->db->where('type', 'category');
    $this->db->where('ci_relationship.term_id', $id);

    $query = $this->db->get();

    if($query->num_rows() > 0)
    {
        return $query->result();
    } 
    else 
    {
        return false;
    }
}