Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-16-2010, 05:51 PM   PM User | #1
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
Adding optional parameters to my function

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:

PHP Code:
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_urlPHP_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?
htcilt is offline   Reply With Quote
Old 03-16-2010, 05:56 PM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Quote:
Originally Posted by htcilt View Post
modify the function to take 1 mandatory parameter and 2 optional.
Code:
function clearFilter($clearMe, $opt1 = false, $opt2 = false)
{
    if ($opt1)
    {
         [optional code here]
    }

    if ($opt2)
    {
         [optional code here]
    }
    [the rest of your code here]
}
MattF is offline   Reply With Quote
Old 03-16-2010, 06:25 PM   PM User | #3
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
excellent! thanks very much
htcilt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:28 AM.


Advertisement
Log in to turn off these ads.