有没有办法在分页时保留我的 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
如何在我的分页类中创建一个指向页面的链接,该页面上有不同的页码,但仍保留 url 的其他部分?
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_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 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!