note 39519 added to function.parse-str
| From: | (jpatsiluethdotcom) | Date: | Sat, 31 Jan 2004 14:45:35 +0000 |
| Subject: | note 39519 added to function.parse-str | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64477@lists.php.net to get a copy of this message | ||
From Juan Prada, Cangas de Onis, Asturias (Spain) [aka countach in Usenet]
Sometimes we work with a lot of GET parameters around.
Normally, in each <A HREF="?... we have to rewrite
ALL parameters that needed in the script,
so that can become a nightmare when adding or removin'
a var or entity, as we have to review all HREF tag to fix it.
With this utility, we'll simply add or update a single
parameter or a list of parameters on the actual
parameter list.
Examples:
Our URL ($_SERVER["QUERY_STRING"]) is showing:
?a=1&b=2&c=3
Normally, when 'painting' a HREF that affect one (b) value but must keep the others, we
make this:
<?php
echo '
<a href="?a='.$_GET["a"].'&b='.$my_b_value.'&c='.
$_GET["c"].'">Click here to selfdestruct</a>';
?>
With this new functions, it will simplify:
<?php
$ls_q=f_urlquery_setvar('b', $my_b_value);
echo '<a href="?'.$ls_q.'">';
// Result: [$my_b_value='hello']
// http://www.foo.com/?a=1&b=hello&c=3
?>
or...
<?php
$ls_q=f_urlquery_update('b='.$my_b_value);
echo '<a href="?'.$ls_q.'">';
// Result: [$my_b_value='hello']
// http://www.foo.com/?a=1&b=hello&c=3
?>
f_urlquery_update() supports multi-values. Take a look:
<?php
$ls_q=f_urlquery_update('d=1223&a=4&b='.$my_b_value);
echo '<a href="?'.$ls_q.'">';
// Will add a 'd' parameter with d=1223,
// modify the 'a' and also the 'b' like the other ones...
// Result: [$my_b_value='hello']
// http://www.foo.com/?a=4&b=hello&c=3&d=1223
?>
THIS ARE THE FUNCTIONS:
<?php
//-----------------------------------
// Update parameter list with another list, adding
// it or modifying it if exists each parameter or not...
//
function f_urlquery_update($as_newparms, $as_parms='') {
if (empty($as_parms)) $as_parms=urldecode($_SERVER["QUERY_STRING"]);
$raw_new_input_items = split("&", $as_newparms);
foreach ($raw_new_input_items as $new_input_item) {
$new_itemPair = split("=", $new_input_item);
$as_parms = f_urlquery_setvar($as_parms, $new_itemPair[0], $new_itemPair[1]);
}
return $as_parms;
}
//--------------
// Modify or add a single parameter in the URL query given
//
function f_urlquery_setvar($as_parms, $as_varname, $as_value) {
$raw_input_items = split("&", $as_parms);
$li_total=0;
$lb_modif=false;
$ls_ret='';
foreach ($raw_input_items as $input_item) {
$itemPair = split("=", $input_item);
if ($itemPair[0] == $as_varname) {
$itemPair[1] = $as_value;
$lb_modif=true;
}
if (strlen($ls_ret)>1) $ls_ret.='&';
$ls_ret.=$itemPair[0].'='.$itemPair[1];
$li_total++;
}
if (!$lb_modif) $ls_ret=$as_parms.'&'.$as_varname.'='.$as_value;
return $ls_ret;
}
?>
Hope will help
Juan Prada [aka countach on UseNet]
WARNING !!
This note was made while listening Paul Oakenfold's new album "Great Wall" .... COOOL
:D
----
Manual Page -- http://www.php.net/manual/en/function.parse-str.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39519
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39519&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39519&report=yes
Search -- http://master.php.net/manage/user-notes.php