php 如何使用 $_SERVER['QUERY_STRING']
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4856480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to work with $_SERVER['QUERY_STRING']
提问by Muiter
How to work with $_SERVER['QUERY_STRING'] and pagination?
如何使用 $_SERVER['QUERY_STRING'] 和分页?
When my table is sorted by this link:
当我的表格按此链接排序时:
<a href="'.$_SERVER['PHP_SELF'].'?sort_name=name&sort=asc" title="'.$lang['sorteer_asc'].'"></a>
My url becomes: relation.php?sort_name=adres&sort=asc
我的网址变成:relation.php?sort_name=adres&sort=asc
The I use an pagination link:
我使用分页链接:
echo '<a href="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&page='.$i.'">'.$i.'</a> ';
And the url becomes: relation.php?sort_name=adres&sort=asc&page=2
网址变为:relation.php?sort_name=adres&sort=asc&page=2
So far so good but when browsing to other pages it can be as long as: relation.php?sort_name=adres&sort=asc&page=2&page=3&page=14&page=23&page=27
到目前为止一切顺利,但在浏览其他页面时,它可以是:relation.php?sort_name=adres&sort=asc&page=2&page=3&page=14&page=23&page=27
The age keeps appearing because of the $_SERVER['QUERY_STRING'], how can I clean up my url with only keeping the last page and ?sort_name=adres&sort=asc.
由于 $_SERVER['QUERY_STRING'],年龄不断出现,我该如何清理我的 url,只保留最后一页和 ?sort_name=adres&sort=asc。
Or do you suggest an other solution of ordering and pagination?
或者您是否建议其他排序和分页解决方案?
回答by mario
Instead of reusing QUERY_STRING
, you should assemble it anew with http_build_query()
.
不是重用的QUERY_STRING
,你应该重新组装起来http_build_query()
。
// Merge $_GET with new parameter
$QS = http_build_query(array_merge($_GET, array("page"=>2)));
// You should apply htmlspecialchars() on the path prior outputting:
echo "<a href='" . htmlspecialchars("$_SERVER[PHP_SELF]?$QS") . "'> $i </a>";
Thus you have all current $_GET
parameters included, but can add or replace entries with new values. And it's ensured that each appears only once.
因此,您包含所有当前$_GET
参数,但可以使用新值添加或替换条目。并且确保每个只出现一次。
回答by SergeS
Create a link builder ( simple php whitch make array( "a" => "b", "c" => "d" ) into ?a=b&c=d and rebuild it each time ( eg. put there "sort_name" => ... , "sort" => ..., "page" => ... )
创建一个链接构建器(简单的 php 使 array( "a" => "b", "c" => "d" )变成 ?a=b&c=d 并每次都重建它(例如放在那里“sort_name”= > ... , "sort" => ..., "page" => ... )
If you still want to use QUERY_STRING - check if it contains &page=... and replace it ( both made by regexp )
如果您仍然想使用 QUERY_STRING - 检查它是否包含 &page=... 并替换它(均由 regexp 制作)
回答by Marc
Don't use QueryString, just create a variable at the beginning of your script:
不要使用 QueryString,只需在脚本开头创建一个变量:
$pagerUrl = $_SERVER['PHP_SELF'].'?sort_name='.$_GET['sort_name']&sort=$_GET['sort'];
And use it in your link:
并在您的链接中使用它:
echo '<a href="'.$pagerUrl.'&page='.$i.'">'.$i.'</a> ';