如何使用$ _SERVER ['QUERY_STRING'] [英] How to work with $_SERVER['QUERY_STRING']

查看:83
本文介绍了如何使用$ _SERVER ['QUERY_STRING']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用$ _SERVER ['QUERY_STRING']和分页?

How to work with $_SERVER['QUERY_STRING'] and pagination?

通过此链接对我的表格进行排序时:

When my table is sorted by this link:

<a href="'.$_SERVER['PHP_SELF'].'?sort_name=name&sort=asc" title="'.$lang['sorteer_asc'].'"></a>

我的网址变为:Relation.php?sort_name = adres& sort = asc

My url becomes: relation.php?sort_name=adres&sort=asc

我使用分页链接:

echo '<a href="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&page='.$i.'">'.$i.'</a> '; 

URL变为:Relation.php?sort_name = adres& sort = asc& page = 2

And the url becomes: relation.php?sort_name=adres&sort=asc&page=2

到目前为止还不错,但是当浏览到其他页面时,它的长度可以是: lation.php?sort_name = adres& sort = asc& page = 2& page = 3& page = 14& page = 23& page = 27

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

由于$ _SERVER ['QUERY_STRING'],年龄不断出现,如何仅保留最后一页和?sort_name = adres& sort = asc来清理网址.

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.

还是您建议使用其他排序和分页解决方案?

Or do you suggest an other solution of ordering and pagination?

推荐答案

代替重用QUERY_STRING,您应该使用

Instead of reusing QUERY_STRING, you should assemble it anew with 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>";

因此,您已经包括了所有当前的$_GET参数,但是可以使用新值添加或替换条目.并且确保每个仅出现一次.

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.

这篇关于如何使用$ _SERVER ['QUERY_STRING']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆