Post by [dmsuperman] on Apr 14, 2006 1:05:50 GMT
/*
string buildUrl ( [bool display_query_string [, array/string get_keys [, array/string get_values [, array/string get_keys_to_remove ]]]] )
buildUrl returns a string that contains a relative url containing data that you desire entered
display_query_string will return the query string for the url you build if it is set to anything but false
get_keys is an array (or string for a single element) of keys for query string to be built
get_values is an array (or string for a single element) of values to match the keys you've given
get_keys_to_remove is an array (or string for a single element) of keys to remove from the query string you are building
*/
function buildUrl($display_query_string=true, $get_keys="", $get_values="", $get_keys_to_remove=""){
$query_string = $_SERVER["QUERY_STRING"];
if(!is_array($get_keys)){
$get_keys = array($get_keys);
}
if(!is_array($get_values)){
$get_values = array($get_values);
}
if(!is_array($get_keys_to_remove)){
$get_keys_to_remove = array($get_keys_to_remove);
}
$current_queries = array();
$new_queries = array();
$num_new_queries = count($get_keys);
foreach($_GET as $key => $value){
if(!in_array($key, $get_keys_to_remove)){
$current_queries[$key] = $value;
}
}
for($x = 0; $x < $num_new_queries; $x++){
$new_queries[$get_keys[$x]] = $get_values[$x];
}
$all_queries = array_merge($current_queries, $new_queries);
$query_string = "";
foreach($all_queries as $key => $value){
$query_string .= $key . "=" . $value . "&";
}
$query_string = substr($query_string, 0, strlen($query_string)-1);
if($display_query_string){
if($query_string!=""){
return($_SERVER["PHP_SELF"]."?".$query_string);
}else{
return($_SERVER["PHP_SELF"]."?");
}
}else{
return($_SERVER["PHP_SELF"]);
}
}
The comment pretty much explains it, you use it to build a URL for links or forms, useful for sites where you're passing around values in the query string and want some kept throughout and some not.
string buildUrl ( [bool display_query_string [, array/string get_keys [, array/string get_values [, array/string get_keys_to_remove ]]]] )
buildUrl returns a string that contains a relative url containing data that you desire entered
display_query_string will return the query string for the url you build if it is set to anything but false
get_keys is an array (or string for a single element) of keys for query string to be built
get_values is an array (or string for a single element) of values to match the keys you've given
get_keys_to_remove is an array (or string for a single element) of keys to remove from the query string you are building
*/
function buildUrl($display_query_string=true, $get_keys="", $get_values="", $get_keys_to_remove=""){
$query_string = $_SERVER["QUERY_STRING"];
if(!is_array($get_keys)){
$get_keys = array($get_keys);
}
if(!is_array($get_values)){
$get_values = array($get_values);
}
if(!is_array($get_keys_to_remove)){
$get_keys_to_remove = array($get_keys_to_remove);
}
$current_queries = array();
$new_queries = array();
$num_new_queries = count($get_keys);
foreach($_GET as $key => $value){
if(!in_array($key, $get_keys_to_remove)){
$current_queries[$key] = $value;
}
}
for($x = 0; $x < $num_new_queries; $x++){
$new_queries[$get_keys[$x]] = $get_values[$x];
}
$all_queries = array_merge($current_queries, $new_queries);
$query_string = "";
foreach($all_queries as $key => $value){
$query_string .= $key . "=" . $value . "&";
}
$query_string = substr($query_string, 0, strlen($query_string)-1);
if($display_query_string){
if($query_string!=""){
return($_SERVER["PHP_SELF"]."?".$query_string);
}else{
return($_SERVER["PHP_SELF"]."?");
}
}else{
return($_SERVER["PHP_SELF"]);
}
}
The comment pretty much explains it, you use it to build a URL for links or forms, useful for sites where you're passing around values in the query string and want some kept throughout and some not.