doqo89924 2016-09-26 09:21
浏览 51

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

I create a pagination with codeigniter

first i take the category id ($id) to display the related articles under that category, and because the menu can has the subcategories i make the id list in an array to select post from that array:this is the link from the menu:

       <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>

So on clicking the link above it displays the articles from that category correctly for the first page of the pagination but when i click on next link of the pagination it works but then it changes the ID of the category so articles are not queried : code for method category in the article controllers is as below:

    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');
        }

}

This is the code from the article model:

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

    }
  • 写回答

1条回答 默认 最新

  • duancan2539 2016-09-27 06:39
    关注

    Try to add category ID to pagination base URL:

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图