沃梦达 / 编程问答 / php问题 / 正文

分页不正确显示页码 Codeigniter

Pagination do not correct display page numbers Codeigniter(分页不正确显示页码 Codeigniter)

本文介绍了分页不正确显示页码 Codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器功能

function test($start_from = 0)
{
    $this->load->library('pagination');

    $data = array();

    $per_page = 3;
    $total = $this->activity_model->count_by();

    $config['base_url'] = base_url() . 'test';
    $config['total_rows'] = $total;
    $config['per_page'] = $per_page;
    $config['uri_segment'] = 2;
    $config['num_links'] = 2;
    $config['use_page_numbers'] = TRUE;

    $data['follow'] = $this->activity_model->get($per_page, $start_from);

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

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

    $this->load->view('front_end/test' ,$data);
}

我的路线:

 $route['test'] = "user_activity/test";
 $route['test/(:any)'] = "user_activity/test/$1";

型号:

 function get($limit,$start_from)
 {
 $sql = "SELECT * FROM user_follow LIMIT $start_from, $limit";

  $query = $this->db->query($sql);
  return $query->result_array();
 }

问题是我有分页 1,2,3,4,5.... 并且在每个页面中我显示 3 个项目.我想在 url 中这样做它显示我的页码 1,2,3,4,5

Problem is that I have pagination 1,2,3,4,5.... and in every page I display 3 items. I want to do that in url it show my page numbers 1,2,3,4,5

当我点击第二页网址时显示 3当我点击第三页 url 显示 6 等等 +3

When I click second page url show 3 When I click third page url show 6 and so on +3

有可能吗,我花了几个小时在互联网上寻找建议,但我所理解的没有 [code]$config['use_page_numbers'] = TRUE;[/code] 做我需要的,但在我的情况下它仍然不起作用.

is it possible, I spend hours for looking advice on internet but nothing as I understand [code]$config['use_page_numbers'] = TRUE;[/code] do what I need but in my case it still do not work.

也许你可以建议任何图书馆?

Maybe you can advice any library ?

推荐答案

在 Pagination 类 (/system/libraries/Pagination.php) 中进行以下更改,使其使用页码而不是偏移量.

Make the following changes in Pagination class (/system/libraries/Pagination.php) so that it uses page numbers instead of offsets.

旧(第 146-153 行):

if ($CI->uri->segment($this->uri_segment) != 0)
{
     $this->cur_page = $CI->uri->segment($this->uri_segment);

    // Prep the current page - no funny business!
    $this->cur_page = (int) $this->cur_page;
} 

新:

在 if 语句中添加else"选项以确保默认为;页 = 1.

Add ‘else’ option to if-statement to make sure default is; page = 1.

if ($CI->uri->segment($this->uri_segment) != 0)
{
    $this->cur_page = $CI->uri->segment($this->uri_segment);

    // Prep the current page - no funny business!
    $this->cur_page = (int) $this->cur_page;
}
else
{
    $this->cur_page = 1;
} 

旧(第 175 行):

$this->cur_page = floor(($this->cur_page/$this->per_page) + 1); 

新:

简单地注释掉这一行,以便当前页面服从控制器/URI.

Simply comment out this line so current page obeys controller/URI.

//$this->cur_page = floor(($this->cur_page/$this->per_page) + 1); 

旧(第 206 行):

$i = $uri_page_number - $this->per_page; 

新:

上一页应始终为当前页减 1.

Previous page should always be current page subtracted by 1.

$i = $uri_page_number - 1; 

旧(第 230 行):

if ($this->cur_page == $loop) 

新:

缺少分页的 URI 应视为第 1 页.

URIs missing pagination should be considered page 1.

if ($this->cur_page == $loop || ($this->cur_page == 1 && $this->cur_page == $loop)) 

旧(第 238-247 行):

if ($n == '' && $this->first_url != '')
{
    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
   $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
}

新:

页面 URL 应使用页码而不是偏移量.

Page URLs should use page numbers and not offsets.

if ($n == '' && $this->first_url != '')
{
    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
    $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
} 

旧(第 256 行):

$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;

新:

下一页应该总是当前页和 1 的总和.

Next page should always be the sum of current page and 1.

$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page + 1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close; 

旧(第 262 行):

$i = (($num_pages * $this->per_page) - $this->per_page); 

新:

最后一页应该是总页数.

Last page should be the total number of pages.

$i = $num_pages; 

用新行替换所有旧行.确保在更改之前备份文件.

Replace all the old lines with new lines. Make sure you do a backup of file before changing.

希望这有帮助:)

您需要更新您的控制器功能测试,例如:

You need to update your controller function test like :

function test($start_from = 0)
{
    $this->load->library('pagination');

    $data = array();

    $per_page = 3;
    $total = $this->activity_model->count_by();

    $config['base_url'] = base_url() . 'test';
    $config['total_rows'] = $total;
    $config['per_page'] = $per_page;
    $config['uri_segment'] = 2;
    $config['num_links'] = 2;
    $config['use_page_numbers'] = TRUE;

    $start = $per_page * ($start_from-1);

    $data['follow'] = $this->activity_model->get($per_page, $start);

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

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

    $this->load->view('front_end/test' ,$data);
}

这里我添加了一个新变量 $start,它是 $per_page * ($start_from-1).现在将此 $start 作为参数传递给模型.

Here i have added a new variable $start which is $per_page * ($start_from-1). Now pass this $start as argument to model.

这样做是将每页的项目数乘以(当前页码 -1).这意味着如果您的每页项目是 10 并且您在第二页 $start = 10 *(2-1) 给出 10.所以你的结果将从 10,20 开始

What this do is multiply the number of items per page with (current page number -1 ) .This means if your items per page is 10 and you are on the second page the $start = 10 *(2-1) which gives 10. So your result will start from 10,20 and so one

希望这有帮助:)

这篇关于分页不正确显示页码 Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:分页不正确显示页码 Codeigniter

基础教程推荐