【问题标题】:Pagination current page incorrect when 'use_page_numbers''use_page_numbers' 时分页当前页面不正确
【发布时间】:2023-03-24 15:02:01
【问题描述】:

我在我的网站上使用 CI 分页库,现在将 $config['use_page_numbers'] 设置为 TRUE,当前页面始终是第一个。一切正常,除了这个。

其他设置:

$config['uri_segment'] = 2;
$config['prefix'] = "p";
$route['mypage/p(:num)'] = "citate/index/$1";

这是计算当前页面的函数(输出正确)。我在第一页时返回 1,在第三页时返回 3,依此类推:

function getPagCurr($offset, $limit){
   if( ! $offset) return $page = 1;
   else return ($offset/$limit)+1;
}

...但是,它不起作用。

我尝试手动设置,只是为了测试,$config['cur_page'] = 2 的值(所以这意味着第二个链接应该被认为是活动的)但根本没有改变。

CI 版本是最新的。


LE:解决方案

似乎前缀是这里的问题。根据我的实际配置,链接将类似于 www.site.com/mypage/p2,它不起作用。 工作链接是 www.site.com/mypage/p/2/,uri_segment = 3 和路由 mypage/p/(:num)。 但是,我真的很想拥有第一个链接结构,所以这是我的解决方案(不是一个好的解决方案,因为您必须修改一些系统库代码):

Pagination.php(开始第 166 行):

// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
   $this->cur_page = $base_page;
}

..改为:

// Set current page to 1 if using page numbers instead of offset
if ($this->use_page_numbers AND $this->cur_page == 0)
{
    $current_page = $CI->uri->segment($this->uri_segment); //get pNUM
    $current_page = substr($current_page, 1); //remove prefix
    $this->cur_page = $current_page; //set current page
}

...现在它可以工作了!

如果有人有更好的解决方案,请告诉! 谢谢。

【问题讨论】:

    标签: php codeigniter pagination codeigniter-2 current-page


    【解决方案1】:

    是的,你是对的,它不会起作用,因为你的片段有一个 p(p2)

    为此,您必须修改核心,但我会说不要修改核心,只需扩展分页类并使用以下代码修改代码:

    添加一个新的类变量

    var $use_rsegment       = FALSE;
    

    然后在第 157 行附近修改create_links()

    //add
    $_uri_segment = 'segment';
    if($this->use_rsegment){
        $_uri_segment = 'rsegment';
    }
    //modify
    if ($CI->uri->$_uri_segment($this->uri_segment) != $base_page)
    {
         $this->cur_page = $CI->uri->$_uri_segment($this->uri_segment);
    
         // Prep the current page - no funny business!
         $this->cur_page = (int) $this->cur_page;
     }
    

    uri rsegment 是新的路由段,现在像这样设置分页配置

    $config['use_rsegment'] = TRUE;
    $this->pagination->initialize($config);
    

    因此,您可以在需要时同时使用这两个选项。当你有路由设置 rsegment true

    【讨论】:

    • 道歉我没有完全注意到 p(:num) 中的“p”
    • 我还没有测试过这个,但可能会工作。我没有修改系统中的核心分页库,我只是在应用程序/库中用修复覆盖它..所以它有点像。谢谢!
    猜你喜欢
    • 2018-05-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多