View previous topic :: View next topic |
Author |
Message |
Galois Smarty Rookie
Joined: 02 May 2003 Posts: 16
|
Posted: Wed Jun 18, 2003 2:28 am Post subject: appending to urls |
|
|
Is there a smarty funnction that will append vars onto the end of a url?
I have a url that is either
index.php
or
index.php?id=12
and I want to add on
order=desc
to get either
index.php?order=desc
or
index.php?id=12&order=desc
I suspect there is a function that already exists, but I can't find it
thanks |
|
Back to top |
|
gianni Smarty Rookie
Joined: 14 May 2003 Posts: 29 Location: Bari, Italy
|
Posted: Wed Jun 18, 2003 5:29 pm Post subject: |
|
|
In your php you can do as follows:
suppose you have a url in the variable $link, before appending vars insert this code:
Code: | if (substr($link , -4) == '.php')
{
$link .= "?";
}
else
{
$link .= "&";
} |
|
|
Back to top |
|
Galois Smarty Rookie
Joined: 02 May 2003 Posts: 16
|
Posted: Wed Jun 18, 2003 6:34 pm Post subject: hmm |
|
|
no fancy smancy smarty function then, eh?
if I use this code snippet I could potentially end up with
index.php?id=12&order=desc&order=desc
the second time I run the function
this is from phplib, but still doesn't solving defining the same vars over and over again
Code: |
function add_query($qarray) {
global $PHP_SELF;
global $QUERY_STRING;
if ((isset($QUERY_STRING) && ("" != $QUERY_STRING))
|| ($this->mode == "get")) {
$sep_char = "&";
} else {
$sep_char = "?";
}
$qstring = "";
while (list($k, $v) = each($qarray)) {
$qstring .= $sep_char . urlencode($k) . "=" . urlencode($v);
$sep_char = "&";
}
return $qstring;
} |
|
|
Back to top |
|
Galois Smarty Rookie
Joined: 02 May 2003 Posts: 16
|
Posted: Wed Jun 18, 2003 7:37 pm Post subject: better version |
|
|
This one works, but a bit more of a hack. It completely replaces the query string and merges $_GET with whatever you want. I'm still surprised there isn't a smarty function that does this. . . .
Code: |
function replace_query($qarray) {
// merge the arrays together
$get = array_merge($_GET,$qarray);
// build new query string
$qstring = "";
$sep_char = "?";
while (list($k, $v) = each($get)) {
$qstring .= $sep_char . urlencode($k) . "=" . urlencode($v);
$sep_char = "&";
}
return $qstring;
}
$new_vars = array ("foo" => "bar",
"one" => "two",
"again" => "yes");
echo "<a href=\"index.php". replace_query($new_vars) . "\" > new link</a> ";
|
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jun 18, 2003 8:35 pm Post subject: |
|
|
You'd have to make one. something like:
{$link|url_add:"foo=bar"}
and in PHP you can do it quick & dirty with a ternary operator:
Code: | $link =
(strpos('?', $link) === false)
? $link . '?' . $add_var
: $link . '&' $add_var;
|
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jun 18, 2003 8:54 pm Post subject: |
|
|
As a general comment, you have to always be careful when you are considering and modifying only portion of a larger string.
For example, Mohrt's suggestion is to check for the presence of "?" in the URL -- in virtually all cases, this will work as expected, but there are some boundary cases where this is insufficient (eg. http://strange/url?itis=true/thatwill?haunt=you).
There are exisitng PHP functions to help you slice & dice urls (eg. parse_url) -- wrap what you need in a modifier, similar to Mohrt's (or use his with the understanding that it works for a vast number of possible cases).
BUT
...many people will tell you that building url strings is an application level function and should not be done in template but instead in your application. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jun 18, 2003 10:15 pm Post subject: |
|
|
boots wrote: | ...many people will tell you that building url strings is an application level function and should not be done in template but instead in your application. |
This is generally true, but there are cases when something like this could be part of the presentation, such as a javascripted image viewer that needs to tack something onto a URL. In any event, the actual logic is still kept out of the template with a custom function.
Monte |
|
Back to top |
|
|