I'm not far from a complete newbie with PHP and I wonder how to change only
the value of one variable in the URL request, when I don't know the others ?
example :
http://MyWebSite.com/MyPage.php?var1=12&var22=xxx&var147=444&var87=aaaa
I only would like to add 1 to var147, without changing anything else in the
string, and I don't know how to do this in PHP.
Thanks for any help !
Christophe
The simplest solution that comes to mind is something like:
$url = "http://www.mysite.com/page.php?var1=" . $_GET[var1] .
"&var22=" . $_GET[var22] . "&var147=" . ($_GET[var147] + 1) .
"&var87=" . $_GET[var87;
header ("location:$url");
All this does is extract the $_GET values and performs the numerical
calculation, Of course you will likely want to add some error
trapping and redirection to a default page if there is an error.
From the PHP Manual:
Example 135. Traversing $_POST with each()
copy to clipboard
echo "Values submitted via POST method:<br />\n";
reset ($_POST);
while (list ($key, $val) = each ($_POST)) {
echo "$key => $val<br />\n";
}
So you could use something like (untested)
$url = basename($PHP_SELF) . "?";
reset($_GET);
while (list ($key, $val) = each ($_GET)) {
if ($key == var147) {
$url .= "$key=" . ($val + 1) . "&";
} else {
$url .= "$key=$val&";
}
}
um....I'm not sure what you mean. surely just requesting:
http://MyWebSite.com/MyPage.php?var1=12&var22=xxx&var147=445&var87=aaaa
would do the trick? where are you trying to add this from? if you
mean when you pass the variable onto MyPage.php, you want to add one,
then:
$var147 = $_REQUEST['var147'] + 1;
should work?
if you explain a bit more, maybe we can help
hth.
andrew
Anyway, it seems Jim's solution will fit, thanks for your help !
If you are trying to preserve parameters between requests like this then
it points towards the use of session variables. Each page can then
manipulate only the session vars it knows about, the others will be
automatically presewrved for you.
Christophe Cerbourg was scribbeling:
>
http://MyWebSite.com/MyPage.php?var1=12&var22=xxx&var147=444&var87=aaaa
>
> I only would like to add 1 to var147, without changing anything else
> in the string, and I don't know how to do this in PHP.
you might also want to have a look at QUERY_STRING:
http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server
hth andreas
--
collect xul annotations: http://xul.andreashalter.ch/