首页 > 解决方案 > 分页显示相同的结果

问题描述

使用 codeigniter 3 我设置了分页。当我单击下一个链接时,我得到相同的结果减去第一个帖子,我似乎找不到导致这种情况的原因。

我尝试删除偏移量并且不允许数字,但我仍然没有任何变化。

控制器

public function index($offset = 0){

//Pagination
   $config['base_url'] = base_url().'posts/index/';
   $config['total_rows'] = $this->db->count_all('posts');
   $config['uri_segment'] = 3;
   $config['use_page_numbers'] = TRUE;
   $config['per_page'] = 5;
   $config['next_link'] = 'Next';
   $config['prev_link'] = 'Previous';
   $config['display_pages'] = FALSE;



   $this->pagination->initialize($config);

  $data['links']  = $this->pagination->create_links();


  $data['title'] = 'Newest';

  $data['posts'] = $this->post_model->get_posts(FALSE,$config['per_page'],$offset);


  $this->load->view('templates/header');
  $this->load->view('posts/index', $data);
  $this->load->view('templates/footer');

}

模型

public function get_posts($slug = FALSE, $limit=FALSE,$offset = FALSE){


if($limit){
  $this->db->limit($limit,$offset);
}


if($slug === FALSE){
  $this->db->order_by('created_time','DESC');
  $this->db->join('categories','categories.id = posts.category_id');
  $query = $this->db->get('posts');
  return $query->result_array();
}

$query = $this->db->get_where('posts',array('slug'=>$slug));
  return $query->row_array();

}

看法

  <ul class="pagination">

                    <?php echo $links; ?>

                  </ul>

/////////////////更新////////////

这就是我的代码在按照您所说的更新后现在的样子。

public function index($offset=0){

//Pagination
   $config['base_url'] = base_url().'posts/index/';
   $config['total_rows'] = $this->db->count_all('posts');
   $config['uri_segment'] = 3;
   $config['num_links'] = 10;
   $config['per_page']=3;

  $limit = $config['per_page'];
  $offset = ($offset) * ($config['per_page']);

 this->pagination->initialize($config);


  $data['posts'] = $this->post_model->get_posts(FALSE,$limit,$offset);

在视图中我将其设置为

<?php echo $this->pagination->create_links(); ?>

所以现在的问题是,它显示 3 行(结果),当我单击下一步时,它什么也不显示,仅此而已,所以现在我只在第一页上得到 3 个结果,而在下一页上没有。怎么了?

标签: phpcodeigniter

解决方案


索引函数不工作段使用查询字符串


推荐阅读