Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

appending to urls

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Galois
Smarty Rookie


Joined: 02 May 2003
Posts: 16

PostPosted: Wed Jun 18, 2003 2:28 am    Post subject: appending to urls Reply with quote

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
View user's profile Send private message Send e-mail
gianni
Smarty Rookie


Joined: 14 May 2003
Posts: 29
Location: Bari, Italy

PostPosted: Wed Jun 18, 2003 5:29 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website Yahoo Messenger
Galois
Smarty Rookie


Joined: 02 May 2003
Posts: 16

PostPosted: Wed Jun 18, 2003 6:34 pm    Post subject: hmm Reply with quote

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
View user's profile Send private message Send e-mail
Galois
Smarty Rookie


Joined: 02 May 2003
Posts: 16

PostPosted: Wed Jun 18, 2003 7:37 pm    Post subject: better version Reply with quote

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
View user's profile Send private message Send e-mail
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jun 18, 2003 8:35 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jun 18, 2003 8:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jun 18, 2003 10:15 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP