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;
}
$http_host
. Besides, your code is broken. Please fix it so we can reopen it (and I can post my answer)! – Schism Jul 11 at 4:46