问题描述
有没有办法在分页时保留我的 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 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)