...

View Full Version : Adding optional parameters to my function



htcilt
03-16-2010, 06:51 PM
Hi,

I've written a function to clear applied filters from a search form.
Taking ebay as an example, you can add a "Condition" filter to the search for "new" and "used".

My function removes the passed parameter from the URL string, then runs the search again without those filters:


function clearFilter($clearMe) {
$prefix_url = 'http://' . htmlentities($_SERVER['HTTP_HOST'], ENT_QUOTES, 'UTF-8') . htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'); // SITE WEB DOMAIN
$suffix_url = "?" . http_build_query($_GET); // PAGE AND FORM PARAMETERS
$current_url = $prefix_url.$suffix_url; // JOIN THEM
$parsed_url = parse_url($current_url, PHP_URL_QUERY);// SEPARATE EACH PARAMETER E.G. CONDITION
$explode_url = array();
parse_str($parsed_url, $explode_url); // PUT EACH PARAMETER INTO ARRAY

// CHECK WHICH FILTER SHOULD BE RESET
if($clearMe=="condition"){
unset($explode_url['condition']);
} elseif ($clearMe=="pricerange"){
unset($explode_url['pricerange']);
} elseif ($clearMe=="distance"){
unset($explode_url['distance']);
}

$new_url = http_build_query($explode_url);//CONVERT BACK TO A URL
$final_url = $prefix_url . "?" . $new_url;
return $final_url;
}

I now need to clear all filters in one go, so I guess I need to modify the function to take 1 mandatory parameter and 2 optional.

Can anyone help me modify my function?

MattF
03-16-2010, 06:56 PM
modify the function to take 1 mandatory parameter and 2 optional.



function clearFilter($clearMe, $opt1 = false, $opt2 = false)
{
if ($opt1)
{
[optional code here]
}

if ($opt2)
{
[optional code here]
}
[the rest of your code here]
}

htcilt
03-16-2010, 07:25 PM
excellent! thanks very much :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum