我有一个带有分页的博客。除了显示的项目重复外,一切正常。 这些项目在其他页面上随机重复显示。例如,在第二页上它显示第一页上已有的项目。
所以,我的 Controller 中有这个功能。
public function index() {
//Pagination
$config['per_page'] = 4;
$config['num_links'] = 2;
$config['base_url'] = base_url().'blog/index';
$config['use_page_numbers'] = TRUE;
$num_rows = $this->db->get('posts');
foreach ($num_rows->result() as $row) {}
$num_rows = $num_rows->num_rows();
$num_rows = $num_rows;
$config['total_rows'] = $num_rows;
//Primeira página
$config['first_link'] = '<span style="font-weight: bold;color: rgb(225,2,38);"><<</span>';
//Última página
$config['last_link'] = '<span style="font-weight: bold;color: rgb(225,2,38);">>></span>';
//Página anterior
$config['prev_link'] = '<button type="select" class="selectarrowleft" href="#"><i class="fa fa-angle-left" aria-hidden="true"></i></button>';
//Próxima página
$config['next_link'] = '<button type="select" class="selectarrowright" href="#"><i class="fa fa-angle-right" aria-hidden="true"></i></button>';
//Páginas disponiveis
$config['num_tag_open'] = '<div>';
$config['num_tag_close'] = '</div>';
//Página corrente
$config['cur_tag_open'] = '<div id="current">';
$config['cur_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->db->order_by('id', 'DESC');
$data['blog'] = $this->db->get('posts', $config['per_page'], $this->uri->segment(4));
$this->load->view('frontend/template/header');
$this->load->view('frontend/template/nav_bar');
$this->load->view('frontend/pages/blog', $data);
$this->load->view('frontend/template/footer');
}
这是 View 上的代码:
<?php
foreach ($blog->result() as $row) {
// The code for showing my blog articles
}
// pagination links
echo $this->pagination->create_links();
?>
如果我将其设置为 false:
$config['use_page_numbers'] = FALSE;
博客文章显示正确,无重复文章。但我真的想要正确的页码。我能做些什么?我见过很多类似的问题,但我无法解决。
最佳答案
我认为你的问题是$this->uri->segment(4);
,你应该设置它$this->uri->segment(3);
,因为如果你设置了段4,你将什么也得不到,除非你想在URL中得到更多的东西,或者:
public function index($page = 1) { //Default is 1
$page
数字将执行相同的操作,就像获取 uri 段一样。然后在 limit($limit, $start)
方法中设置 $start
变量。
$start = ($page - 1) * $config['per_page'];
$data['blog'] = $this->db->get('posts', $config['per_page'], $start);
关于php - Codeigniter 分页使用页码重复内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45638955/