分页显示相同的结果

时间:2019-02-27 19:04:40

标签: php codeigniter

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

/////////////////// UPDATE ////////////

因此,这就是我所说的代码更新后的样子。

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个结果,在下一页上都没有。 。怎么了?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果$ offset是从{0开始的current_page_number,则定义应为:

//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);

如果您将offset定义为current_page,则从1开始。

public function index($offset = 1){
......
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset - 1) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);

有关“索引函数不起作用,段查询字符串”的问题...

如果您有$ config ['enable_query_strings'] = TRUE,则应获取它

public function index() {
$page = $this->input->get('page');
...
$offset = ($page - 1) * ($config['per_page']);
...