Phasm
12-25-2004, 06:23 AM
We've all used query strings (at least I'm pretty sure), and writing them isn't too bad if they are short. But haven't you always wished there was a better way to append a query string onto a url without using a form with the GET method (I have seen this done, and it weirded me out). Well, maybe you have. Maybe you're lazy like me. Maybe you just want another function to add to your site. If you said yes to one of these questions, I have your solution, and I'm gonna teach it to you all.
function append_query_string($url, $query, $delimiter='&', $look='?')
{
$query2 = "";
if(is_array($query))
{
$i = 1;
foreach($query as $k=>$v)
{
$count = count($query);
if ($i != $count) { $query2 .= $k . "=" . $v . $delimiter; }
else { $query2 .= $k . "=" . $v; }
$i++;
}
}
else {$query2 = $query; }
$ex_url = explode($look, $url);
if (count($ex_url) > 1)
{
$url = $url . $delimiter . $query2;
}
else
{
$url = $url . $look . $query2;
}
return $url;
}
May look intimidating, but it isn't. I'll go through thought by thought.
function append_query_string($url, $query, $delimiter='&', $look='?')
{
$query2 = "";
Ok, for all the novices out there, this delcares the function, and defines the four major variables (and sets delimiter and look to default values). And we also define $query2 for later use.
if(is_array($query))
{
$i = 1;
foreach($query as $k=>$v)
{
$count = count($query);
if ($i != $count) { $query2 .= $k . "=" . $v . "&"; }
else { $query2 .= $k . "=" . $v; }
$i++;
}
}
else {$query2 = $query; }
Now, we want to check if $query is an array. If it is, cycle through it, appended it to $query2, and tag the delimiter on it. While we are doing this, we check to see if the number of articles in the array is met yet, and if it is, we don't appnd the delimiter. If $query is not an array, no need to cycle through it, $query2 is set to $query.
$ex_url = explode($look, $url);
if (count($ex_url) > 1)
{
$url = $url . $delimiter . $query2;
}
else
{
$url = $url . $look . $query2;
}
Now, we need to check if there is already a query string or not. First, we explode the $url into $ex_url, then check for how many parts are in this array. If there is more than one, we have a query string, append the new query to the old with the delmiter. If there is only one part, no need to append with the delimiter and justtag the query string definer and the query string onto the url.
return $url;
}
Return the URL as the value of the function and end it.
Hope that helps at least one person. And I know this function can be improved (I plan on adding support for mod_rewite urls eventually), but on the whole, I am satisfied with it. Post comments, critics, and improvements please.
function append_query_string($url, $query, $delimiter='&', $look='?')
{
$query2 = "";
if(is_array($query))
{
$i = 1;
foreach($query as $k=>$v)
{
$count = count($query);
if ($i != $count) { $query2 .= $k . "=" . $v . $delimiter; }
else { $query2 .= $k . "=" . $v; }
$i++;
}
}
else {$query2 = $query; }
$ex_url = explode($look, $url);
if (count($ex_url) > 1)
{
$url = $url . $delimiter . $query2;
}
else
{
$url = $url . $look . $query2;
}
return $url;
}
May look intimidating, but it isn't. I'll go through thought by thought.
function append_query_string($url, $query, $delimiter='&', $look='?')
{
$query2 = "";
Ok, for all the novices out there, this delcares the function, and defines the four major variables (and sets delimiter and look to default values). And we also define $query2 for later use.
if(is_array($query))
{
$i = 1;
foreach($query as $k=>$v)
{
$count = count($query);
if ($i != $count) { $query2 .= $k . "=" . $v . "&"; }
else { $query2 .= $k . "=" . $v; }
$i++;
}
}
else {$query2 = $query; }
Now, we want to check if $query is an array. If it is, cycle through it, appended it to $query2, and tag the delimiter on it. While we are doing this, we check to see if the number of articles in the array is met yet, and if it is, we don't appnd the delimiter. If $query is not an array, no need to cycle through it, $query2 is set to $query.
$ex_url = explode($look, $url);
if (count($ex_url) > 1)
{
$url = $url . $delimiter . $query2;
}
else
{
$url = $url . $look . $query2;
}
Now, we need to check if there is already a query string or not. First, we explode the $url into $ex_url, then check for how many parts are in this array. If there is more than one, we have a query string, append the new query to the old with the delmiter. If there is only one part, no need to append with the delimiter and justtag the query string definer and the query string onto the url.
return $url;
}
Return the URL as the value of the function and end it.
Hope that helps at least one person. And I know this function can be improved (I plan on adding support for mod_rewite urls eventually), but on the whole, I am satisfied with it. Post comments, critics, and improvements please.