Rebuild query string

Rebuild query string

We can use the following function to rebuild the query string. The function is very useful for pagination or passing variables from one page to another.In this function it is possible to add more than one variable names as functions arguments. This names will be filtered from the new generated query string. We can just add the variable name no need a comma seperated string.

<?php
function rebuild_qs($curr_vars) {
if (!empty($_SERVER[’QUERY_STRING’])) {
$parts = explode(”&”, $_SERVER[’QUERY_STRING’]);
$curr_vars = str_replace(” “, “”, $curr_vars); // remove whitespace
$c_vars = explode(”,”, $curr_vars);
$newParts = array();
foreach ($parts as $val) {
$val_parts = explode(”=”, $val);
if (!in_array($val_parts[0], $c_vars)) {
array_push($newParts, $val);
}
}
if (count($newParts) != 0) {
$qs = “&”.implode(”&”, $newParts);
} else {
return false;
}
return $qs; // this is your new created query string
} else {
return false;
}
}
/* Example:
script.php?ident=1<?php echo rebuild_qs(”ident, submit, var_one”); ?> */
?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.