在分页期间保留url参数 [英] keeping url parameters during pagination
问题描述
在分页时有什么方法可以保留我的GET参数.
Is there any way to keep my GET parameters when paginating.
我的问题是我有几个不同的网址,即
My problem is that I have a few different urls i.e
questions.php?sort=votes&author_id=1&page=3
index.php?sort=answers&style=question&page=4
我应该如何在分页课程中创建指向页面上具有不同页码的页面的链接,但仍然保留网址的其他部分?
How in my pagination class am I supposed to create a link to the page with a different page number on it but yet still keep the other parts of the url?
推荐答案
简而言之,您只需解析URL,然后在最后添加参数,或者如果参数已经存在,则将其替换.
In short, you just parse the URL and then you add the parameter at the end or replace it if it already exists.
$parts = parse_url($url) + array('query' => array());
parse_str($parts['query'], $query);
$query['page'] = $page;
$parts['query'] = http_build_str($query);
$newUrl = http_build_url($parts);
此示例代码需要 PHP HTTP模块 ="http://php.net/http_build_url" rel ="nofollow"> http_build_url
和 http_build_str
.后者可以替换为 http_build_query
;对于第一个,PHP用户空间实现可以在不存在的情况下实现已经安装了模块.
This example code requires the PHP HTTP module for http_build_url
and http_build_str
. The later can be replaced with http_build_query
and for the first one a PHP userspace implementation exists in case you don't have the module installed.
另一种替代方法是使用 Net_URL2
程序包,该程序包提供了各种URL操作的接口:
Another alternative is to use the Net_URL2
package which offers an interface to diverse URL operations:
$op = new Net_URL2($url);
$op->setQueryVariable('page', $page);
$newUrl = (string) $op;
它更具灵活性和表现力.
It's more flexible and expressive.
这篇关于在分页期间保留url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!