Codeigniter 分页后向URL追加变量';s'/第/x页和第x27页;

Codeigniter 分页后向URL追加变量';s'/第/x页和第x27页;,codeigniter,Codeigniter,我使用了几个URI变量来处理表的排序,如下所示 ../page/7/sortby/serial\u number/orderby/desc 如您所见,我还使用了内置的CI分页库。我现在的问题是使用$this->pagination->create_links()创建的链接从URI中去掉排序变量,使得在页面之间维护这些排序选项变得困难 如何将这些变量sortby/foo/orderby/bar附加到分页库创建的链接的URI中?您可以使用base\u url选项,页码段必须是最后一个。这有点烦人,但

我使用了几个URI变量来处理表的排序,如下所示

../page/7/sortby/serial\u number/orderby/desc

如您所见,我还使用了内置的CI分页库。我现在的问题是使用
$this->pagination->create_links()创建的链接从URI中去掉排序变量,使得在页面之间维护这些排序选项变得困难


如何将这些变量
sortby/foo/orderby/bar
附加到分页库创建的链接的URI中?

您可以使用
base\u url
选项,页码段必须是最后一个。这有点烦人,但我认为这是最简单的方法

// Get the current url segments
$segments = $this->uri->uri_to_assoc();

// Unset the "page" segment so it's not there twice
$segments['page'] = null;

// Put the uri back together
$uri = $this->uri->assoc_to_uri($segmenmts);

$config['base_url'] = 'controller/method/'.$uri.'/page/';
// other config here

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

您可以使用
base\u url
选项,页码段必须是最后一个。这有点烦人,但我认为这是最简单的方法

// Get the current url segments
$segments = $this->uri->uri_to_assoc();

// Unset the "page" segment so it's not there twice
$segments['page'] = null;

// Put the uri back together
$uri = $this->uri->assoc_to_uri($segmenmts);

$config['base_url'] = 'controller/method/'.$uri.'/page/';
// other config here

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

我找到了答案,这要感谢卫斯理默奇带领我走向正确的方向。为了始终将页面变量作为uri中的最后一个变量(这在使用CI的分页库时是必需的),我使用了

$totalseg = $this->uri->total_segments();
$config['uri_segment'] = $totalseg;
然后根据卫斯理默奇的想法,我重建了基本url

$segments = $this->uri->uri_to_assoc();
unset($segments['page']);   //so page doesn't show up twice
$uri = $this->uri->assoc_to_uri($segments);
$config['base_url'] = site_url()."/controller/method/".$uri."/page/";
当然,使用所有正确的配置选项初始化分页


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

我找到了答案,多亏卫斯理·穆奇带领我朝着正确的方向前进。为了始终将页面变量作为uri中的最后一个变量(这在使用CI的分页库时是必需的),我使用了

$totalseg = $this->uri->total_segments();
$config['uri_segment'] = $totalseg;
然后根据卫斯理默奇的想法,我重建了基本url

$segments = $this->uri->uri_to_assoc();
unset($segments['page']);   //so page doesn't show up twice
$uri = $this->uri->assoc_to_uri($segments);
$config['base_url'] = site_url()."/controller/method/".$uri."/page/";
当然,使用所有正确的配置选项初始化分页


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

我使用的是ejb的答案,但是

如果出于某种原因,用户在url的页面变量中没有输入数字或负数,我建议在设置$config['uri_segment']之前进行验证,如下所示:

    $totalseg = $this->uri->segment($totalseg)>0 && 
           is_numeric($this->uri->segment($totalseg))?
                        $totalseg : NULL;

我希望它能帮上忙

我使用了弗朗西斯的答案,但是

如果出于某种原因,用户在url的页面变量中没有输入数字或负数,我建议在设置$config['uri_segment']之前进行验证,如下所示:

    $totalseg = $this->uri->segment($totalseg)>0 && 
           is_numeric($this->uri->segment($totalseg))?
                        $totalseg : NULL;

我希望它能帮上忙

谢谢!你让我找到了正确的答案,但事实并非如此。出于这个原因,我发布了我的解决方案,并将其标记为答案,以便其他人可以轻松查看完整答案!你让我找到了正确的答案,但事实并非如此。出于这个原因,我发布了我的解决方案,并将其标记为答案,以便其他人可以轻松地看到完整答案