Codeigniter分页首页始终是当前页 [英] Codeigniter Pagination first page is always current page

查看:112
本文介绍了Codeigniter分页首页始终是当前页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在把头发拔掉.我正在使用CodeIgniters分页库,现在它始终卡在第1页上作为当前页面.我检查了很多StackOverflow问题,但没有其他人遇到相同的问题.

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.

这是我的网址结构

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

当我在视图中回显分页时,它似乎起作用了.每个链接上的网址都是正确的,并且一切正常.最后一个链接显示最后一页的URL,当前页面为1.但是,当我单击第2页或分页中的任何其他页面时,即使URL如下,它仍将第1页显示为当前页面

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

我使用$this->uri->segment(3)来获取数据库查询的页码,因此页码位于正确的段中.只是仔细检查一下,我将$config['uri_segment']值设置为1,2,3,4,5,6只是为了确保.

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.

我在撰写本文时发现了问题,但我仍然感到困惑

然后我认为url本身可能正在发生某些事情,因为我有一条将其定向到控制器中index方法的路由.这是我的路线文件的样子

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

然后,我尝试为分页配置设置base_url,以将其直接发送到索引,如下所示:

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

现在看来工作正常.但是,如何使它与以前的url结构一起使用呢?我只是认为它看起来更好,因此我确实不需要控制器中的方法.我的routes.php文件是否有冲突?

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?

谢谢

推荐答案

定义cur_page并定义controller,如:

public function index($page=''){
    //...
    $page = ($page!='')? $page : 0;
    $config["cur_page"] = $page;

    //...

}

这篇关于Codeigniter分页首页始终是当前页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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