Codeigniter分页:当点击进一步的页面链接时工作,但它改变了我试图用来查询相关文章的类别的ID


Codeigniter pagination: works when clicked on further page link but it changes the ID of the category am try to use to query relate articles

我用codeigniter创建了一个分页

首先,我使用类别id ($id)来显示该类别下的相关文章,并且因为菜单可以有子类别,所以我在数组中创建id列表以从该数组中选择帖子:这是菜单中的链接:

       <li><a href="<?php echo base_url(); ?>article/category/
        <?php echo $category->cat_id;?>">
       <span class="ion-ios7-arrow-right nav-sub-icn"></span>
       <?php echo $category->cat_name;?></a></li>

当点击上面的链接时,它会正确地显示该类别的文章在分页的第一页上但是当我点击下一个分页链接时,它会工作,但是它会改变类别的ID,因此不会查询文章:文章控制器中方法类别的代码如下:

    public function category($x =''){
        $total_rows           = $this->article_model->count();
        $config['total_rows'] =$total_rows;
        $config['per_page']   =$this->limit;
        $config['uri_segment']=4;
        $config['base_url']   = base_url().'article/category';
        $this->pagination->initialize($config);
        if (!empty($x)) {
            $data['categories'] = $this->category_model->listAll();
            $cond['condition']=array('cat_id'=>preg_replace('#[^0-9]#', '', $x));
            $data['index'] = $this->category_model->view($cond);
            $data['view'] = $this->article_model->all_article_by_category($x ,$this->limit);
            $data['page_link'] = $this->pagination->create_links();
            $this->load->view('article/category', $data);
        }else{
            redirect('/home', 'refresh');
        }
}

这是文章模型的代码:

        function all_article_by_category($cat_id, $limit){
        $offset =$this->uri->segment(4);
        $this->db->select(array('art_id','title','description','public_date','image','username','created','cat_id'));
        $this->db->from($this->tbl_article);
        $this->db->join($this->tbl_user, 'tbl_user.user_id = tbl_article.u_id');
        $this->db->where('cat_id', $cat_id);
        $this->db->order_by('created','desc');
        return $this->db->limit($limit, $offset)->get();
    }

尝试将类别ID添加到分页基URL:

$config['base_url']   = base_url().'article/category/' . $x;