Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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]
    }

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    excellent! thanks very much


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •