Skip to main content
added 24 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Add a set of querystring params to a urlURL

Example cases: Need

Need to add x=1x=1, y=2 querystringy=2 and querystring variables to the following urlsURLs:

http://www.foo.com/blah/blah?test=1&abc=2

http://www.foo.com/blah

//-- usage
get_link(array('x' => 1, 'y' => 2, 'z' => 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];
    
    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];
    
    foreach ( $my_params as $param_key => $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }
    
    if ( empty( $querystring ) )
    {
        //-- generates foo.com/blah?x=1&y=2&z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&' . $param_querystring;
    }
    
    return $link;
}

Add a set of querystring params to a url

Example cases: Need to add x=1, y=2 querystring variables to the following urls:

http://www.foo.com/blah/blah?test=1&abc=2

http://www.foo.com/blah

//-- usage
get_link(array('x' => 1, 'y' => 2, 'z' => 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];
    
    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];
    
    foreach ( $my_params as $param_key => $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }
    
    if ( empty( $querystring ) )
    {
        //-- generates foo.com/blah?x=1&y=2&z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&' . $param_querystring;
    }
    
    return $link;
}

Add a set of querystring params to a URL

Example cases:

Need to add x=1, y=2 and querystring variables to the following URLs:

//-- usage
get_link(array('x' => 1, 'y' => 2, 'z' => 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];
    
    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];
    
    foreach ( $my_params as $param_key => $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }
    
    if ( empty( $querystring ) )
    {
        //-- generates foo.com/blah?x=1&y=2&z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&' . $param_querystring;
    }
    
    return $link;
}
added 9 characters in body
Source Link
Don Boots
  • 101
  • 1
  • 1
  • 2

Example cases: Need to add x=1, y=2 querystring variables to the following urls:

http://www.foo.com/blah/blah?test=1&abc=2

http://www.foo.com/blah

//-- usage
get_link(array('x' => 1, 'y' => 2, 'z' => 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];
    
    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];
    
    foreach ( $my_params as $param_key => $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }
    
    if ( empty( $qs$querystring ) )
    {
        //-- generates foo.com/blah?x=1&y=2&z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&' . $param_querystring;
    }
    
    return $link;
}

Example cases: Need to add x=1, y=2 querystring variables to the following urls:

http://www.foo.com/blah/blah?test=1&abc=2

http://www.foo.com/blah

//-- usage
get_link(array('x' => 1, 'y' => 2, 'z' => 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];
    
    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];
    
    foreach ( $my_params as $param_key => $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }
    
    if ( empty( $qs ) )
    {
        //-- generates foo.com/blah?x=1&y=2&z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&' . $param_querystring;
    }
    
    return $link;
}

Example cases: Need to add x=1, y=2 querystring variables to the following urls:

http://www.foo.com/blah/blah?test=1&abc=2

http://www.foo.com/blah

//-- usage
get_link(array('x' => 1, 'y' => 2, 'z' => 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];
    
    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];
    
    foreach ( $my_params as $param_key => $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }
    
    if ( empty( $querystring ) )
    {
        //-- generates foo.com/blah?x=1&y=2&z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&' . $param_querystring;
    }
    
    return $link;
}
Source Link
Don Boots
  • 101
  • 1
  • 1
  • 2

Add a set of querystring params to a url

Example cases: Need to add x=1, y=2 querystring variables to the following urls:

http://www.foo.com/blah/blah?test=1&abc=2

http://www.foo.com/blah

//-- usage
get_link(array('x' => 1, 'y' => 2, 'z' => 'string'));

function get_link( $my_params )
{   
    $param_querystring = "";
    $http_host = $_SERVER['HTTP_HOST'];
    
    //-- get the part of the url before the querystring, if applicable
    $url = explode( '?', $_SERVER['REQUEST_URI'] );

    $request_uri = $url[0];
    $querystring = $url[1];
    
    foreach ( $my_params as $param_key => $param_value )
    {
        $param_querystring .= $param_key . '=' . $param_value;
    }
    
    if ( empty( $qs ) )
    {
        //-- generates foo.com/blah?x=1&y=2&z=string if no
        //-- querystring was present
        $link = $request_uri . '?' . $param_querystring;
    }
    else
    {

        //-- generates foo.com/blah?a=1&b=2&x=1&y=2&z=string if a=1&b=2 
        //-- querystring was already present.
        $link = $request_uri . $querystring . '&' . $param_querystring;
    }
    
    return $link;
}