douhuo1738 2013-09-22 00:19
浏览 34

Codeigniter Pagination第一页始终是当前页面

I am pulling my hair out on this one. I am using CodeIgniters pagination library and right now it is always stuck on page 1 as the current page. I have checked a bunch of StackOverflow questions and I don't have the same problem as anyone else.

Here is my url structure

website.com/leaders/page/[page_number]

Here is the pagination code in my controller

    $this->load->library('pagination');

    $config['per_page'] = $query_config['limit'];
    $config['base_url'] = base_url() . 'leaders/page/';
    $config['total_rows'] = 2000; // I actually use a function for this number
    $config['full_tag_open'] = '<div id="paginate">';
    $config['full_tag_close'] = '</div>';
    $config['first_link'] = '&laquo; First';
    $config['last_link'] = 'Last &raquo;';
    $config['use_page_numbers'] = true;
    $config['uri_segment'] = 3;

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

When I echo the pagination in the view it looks like it works. The urls on each link is correct and everything looks fine. The last link shows the last page url and the current page is 1. However when I click on page 2 or any other page from the pagination, it still shows page 1 as the current page even though the url is as follows

website.com/leaders/page/2

I used the $this->uri->segment(3) to grab the page number for my database queries so the page number is in the right segment. Just to double check I set the $config['uri_segment'] values to 1,2,3,4,5,6 just to make sure.

I found out the problem while writing this but I am still confused

Then I thought maybe there is something going on with the url itself as I have a route directing it to the index method in the controller. Here is what my routes file looks like

routes.php

$route['leaders/page/(:num)'] = 'leaders/index';
$route['leaders/page'] = 'leaders/index';

Then I tried setting the base_url for the pagination config to send it to the index directly like so:

$config['base_url'] = base_url . 'leaders/index';

Now it seems to be working properly. But how do I make it so that it works with the url structure I had before? I just think it looks nicer and I don't really need a method in the controller for this. Is there something conflicting in my routes.php file?

Thanks

  • 写回答

3条回答 默认 最新

  • doufan3408 2013-09-22 07:24
    关注

    Use this in your code, hope this will work-

    if ($this->uri->segment(3) > 0) {
         $offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
     } else {
         $offset = $this->uri->segment(3);
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分