我的网址包含许多我想要保持不变的变量(不要担心它们并不重要)。
假设它包含...
../index.php?id=5
我将如何制作一个只添加的网址
&当前=1
而不是完全替换它?
我想...
../index.php?id=5¤t=1
而不是..
../index.php?current=1
我知道这是一个简单的问题,但这就是为什么我无法弄清楚。
谢谢。
要将参数附加到 URL,您可以执行以下操作:
function addParam( $url, $param ){
if( strrpos( $url, '?' ) === false){
$url .= '?' . $param;
} else {
$url .= '&' . $param;
}
return $url;
}
$url = "../index.php?id=5";
$url = addParam( $url, "current=1");
您应该创建链接以"添加"该参数
<a href="index.php?id=5¤t=1">The Link</a>
然后显然在索引中.php您将在某个地方查找当前变量并执行所需的操作:
<?php
if(isset($_GET['current']) && !empty($_GET['current]) {
// Do stuff here for the 'current' variable
$current = trim($_GET['current']);
}
?>
在您需要$current变量的链接上,我想您可以随意将其放在 href 属性中。对于索引,php文件,所以像这样的东西....
if(isset($_GET['current']))
{
$current = $_GET['current'];
//Do the rest of what you need to do with this variable
}
试试这个:
$givenVar = "";
foreach($_GET as $key=>$val){
$givenVar .= "&".$key."=".$val;
}
$var = "&num=1";
$link = "?".$givenVar."".$var;
echo $link;
您可以将变量添加到href
,
<a href="¤t=1"></a>
当您在地址
../index.php?id=5
相信我,然后去
../index.php?id=5¤t=1
但是,如果您再次单击该链接,您将转到
../index.php?id=5¤t=1¤t=1
实际上,我认为仅附加变量是棘手和不好的做法。
我建议你这样做:
<?php
$query = isset($_GET) ? http_build_query($_GET) . '¤t=1' : 'current=1';
?>
<a href="http://url.com/?<?php echo $query; ?>">A Label</a>
看看 http://us.php.net/manual/en/function.http-build-query.php
我不知道
为什么在地球上你需要这个,但我们来了。这应该可以解决问题。
$appendString = "¤t=1";
$pageURL = $_SERVER["REQUEST_URI"].$appendString;
$_SERVER["REQUEST_URI"] 应该只返回所请求页面的名称,并附加任何其他 GET 变量。另一个字符串应该足够清晰!