php - 分页显示相同的结果
问题描述
使用 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 个结果,而在下一页上没有。怎么了?
解决方案
索引函数不工作段使用查询字符串
推荐阅读
- ruby-on-rails - 在 Ruby on Rails 中缓存类/对象列表
- c++ - H265 编解码器从 hvc1 更改为 hev1
- ionic-framework - 将 Http 响应转换为 Json 对象 Ionic 3
- android - 在android Nougat中安装apk文件时如何解决解析包错误
- androidviewclient - Culebra GUI 运行缓慢
- angular - 如何删除当前点击的html div标签角4
- ios - 在 TableView 上添加搜索栏但没有响应
- replace - 同时替换多个列值
- android - Retrofit2 未找到任何改造注释。(参数#1)
- python - 如何使用 MinMaxScaler sklearn 标准化训练和测试数据