分页不正确显示页码编号 [英] Pagination do not correct display page numbers Codeigniter

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

问题描述

我的控制器函数

 函数测试($ start_from = 0)
{
$ this- > load-> library('pagination');

$ data = array();

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

$ config ['base_url'] = base_url()。 '测试';
$ 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;

型号:

 code> 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



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



是可能的,我花了几个小时在互联网上寻求建议,但没有什么我理解[代码] $ config [' use_page_numbers'] = TRUE; [/ code]做我需要的,但在我的情况下,它仍然不工作。



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

解决方案

在分页类(/system/libraries/Pagination.php)中进行以下更改, 。



OLD(第146-153行):

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

//准备当前页面 - 没有有趣的业务!
$ this-> cur_page =(int)$ this-> cur_page;
}

新:



为if语句添加else选项,以确保默认值为; page = 1。

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

//准备当前页面 - 没有有趣的业务!
$ this-> cur_page =(int)$ this-> cur_page;
}
else
{
$ this-> cur_page = 1;
}

OLD(第175行): >

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

新:



只要注释掉这一行,因此当前页面服从控制器/ URI。

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

旧(第206行)

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

新:



上一页应始终为当前页减去1。

  $ i = $ uri_page_number  -  1; 

OLD(第230行)

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

新功能:



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

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

OLD(第238-247行) / p>

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

新:



网页网址应使用页码而不是偏移量。

  if($ n ==' &&&&&&&&&&&" 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;
}

OLD(第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的总和。

  $ output。= $ this-> next_tag_open 。'  

OLD(第262行)

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

新:



最后一页应为总页数。

  $ i = $ num_pages; 

使用新行替换所有旧行。



希望这有助于:)





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

  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()。 '测试';
$ 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作为模型的参数传递。



这是做每页的项目数乘以(当前页码-1)。这意味着如果你的项目每页为 10 ,您在第二页上的 $ start = 10 *(2-1) 10 。因此,您的结果将从10,20开始,因此一个



希望这有助于:)


My controller function

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

my route :

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

model :

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

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

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

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

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 ?

解决方案

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

OLD (lines 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;
} 

NEW:

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

OLD (line 175):

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

NEW:

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

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

OLD (line 206):

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

NEW:

Previous page should always be current page subtracted by 1.

$i = $uri_page_number - 1; 

OLD (line 230):

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

NEW:

URIs missing pagination should be considered page 1.

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

OLD (line 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;
}

NEW:

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

OLD (line 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;

NEW:

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; 

OLD (line 262):

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

NEW:

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.

Hope this helps :)

EDIT:

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

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

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

Hope this helps :)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆