【问题标题】:Codeigniter Pagination repeated content with Page numbersCodeigniter Pagination用页码重复内容
【发布时间】:2018-01-20 04:37:58
【问题描述】:

我有一个带有分页的博客。除了显示的项目重复外,一切正常。 这些项目在其他页面上随机重复显示。例如,在第二页上显示的项目已经在第一页上。

所以,我的控制器中有这个功能。

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

这是视图上的代码:

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

博客文章显示正确,没有重复文章。但我真的很想拥有正确的页码。我能做些什么?我见过很多类似的问题,但我无法解决。

【问题讨论】:

  • 这是正确的吗? foreach ($num_rows->result() as $row) {} $num_rows = $num_rows->num_rows(); $num_rows = $num_rows;
  • 项目是否在同一页面上重复?下一页?是重复的(连同新项目)还是相同的项目继续显示?您没有正确提供偏移量。
  • 尝试 $num_rows = $num_rows->num_rows();在具有不同变量名称的 foreach() 循环上方,例如 $totalRow = $num_rows->num_rows();和 $config['total_rows'] = $totalRow;
  • 这里 $num_rows 变量可能与mysql结果的总行数发生冲突
  • 项目在其他页面上随机重复显示。例如,在第二页上它会显示一个已经在第一页上的项目。

标签: php codeigniter pagination


【解决方案1】:

我认为你的问题是$this-&gt;uri-&gt;segment(4);,你应该设置它$this-&gt;uri-&gt;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);

【讨论】:

    猜你喜欢
    • 2015-01-27
    • 2014-03-05
    • 1970-01-01
    • 2021-09-26
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2022-01-15
    相关资源
    最近更新 更多