note 102745 added to reserved.variables.request
| From: | e dot reggi at afterbabel dot net | Date: | Thu, 03 Mar 2011 11:19:42 +0000 |
| Subject: | note 102745 added to reserved.variables.request | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177523@lists.php.net to get a copy of this message | ||
I've implemented John Galt's function as to be able to pass a multi-dimensional array as
input parameter. This lets you add, change, or even delete several $_GET's variables at a time.
<?php
function update_gets($parameter) {
$output = "?";
$firstRun = true;
for($index = 0; $index < count($parameter); $index++) {
foreach($_GET as $key=>$val) {
if($key != $parameter[$index][0]) {
if(!isset($_GET[$key]) && $parameter[$index][1] != NULL) {
if(!$firstRun) {
$output .= "&";
}
else {
$firstRun = false;
}
$output .= $key."=".urlencode($val);
$_GET[$key] = $val;
}
}
else {
if($parameter[$index][1] != NULL) {
if(!$firstRun) {
$output .= "&";
}
else {
$firstRun = false;
}
$output .= $key."=".urlencode($parameter[$index][1]);
$_GET[$key] = $parameter[$index][1];
}
else {
unset($_GET[$key]);
}
}
}
if(!isset($_GET[$parameter[$index][0]]) && $parameter[$index][1] != NULL) {
if(!$firstRun) {
$output .= "&";
}
else {
$firstRun = false;
}
$output .= $parameter[$index][0]."=".urlencode($parameter[$index][1]);
$_GET[$parameter[$index][0]] = $parameter[$index][1];
}
}
return htmlentities($output);
}
?>
__________________________
#########################################
#########################################
## ##
## Structure of $parameter array ##
## ##
#########################################
#########################################
<?php
$parameter = array {
[0] => array {
[0] => $key;
[1] => $value;
}
...
[n] => array {
[0] => $key;
[1] => $value;
}
}
?>
#################################################################
#################################################################
## ##
## Example Usage ##
## ##
## WARNING: THIS FUNCTION WORKS EXACTELY LIKE JOHN ##
## GALT'S ONE, WHICH MEANS YOU CAN USE IT ##
## THE SAME WAY, JUST PASSING AN ARRAY, ##
## STRUCTURED AS SHOWN ABOVE, AS INPUT ##
## ARGUMENT. ##
## ##
#################################################################
#################################################################
<?php
#Deleting an existent $_GET's variable, which name is $key:
$parameter[0][0] = $key;
$parameter[0][1] = NULL;
update_gets($parameter);
#Adding or Changing an existent $_GET's variable, which name is $key:
$parameter[0][0] = $key;
$parameter[0][1] = $new_value;
update_gets($parameter);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 151.20.91.219
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.request.php
Edit -- https://master.php.net/note/edit/102745
Del: integrated -- https://master.php.net/note/delete/102745/integrated
Del: useless -- https://master.php.net/note/delete/102745/useless
Del: bad code -- https://master.php.net/note/delete/102745/bad+code
Del: spam -- https://master.php.net/note/delete/102745/spam
Del: non-english -- https://master.php.net/note/delete/102745/non-english
Del: in docs -- https://master.php.net/note/delete/102745/in+docs
Del: other reasons-- https://master.php.net/note/delete/102745
Reject -- https://master.php.net/note/reject/102745
Search -- https://master.php.net/manage/user-notes.php