Emre Sokullu
Oct 14
How to Add a GET Variable to A Href
Tue 14 Oct 2008 02:01:14 | 2 comments

Hi folks, just wanted to share a quick tip I've found while hacking PHP Smarty. First let me state the problem; on grou.ps home page, we allow our visitors to change the language of the site by clicking the flag buttons. What these nice flag buttons do is simply to add a ?lang=X suffix at the end of the page; it's pretty simple as shown in this example: <a xhref="?lang=en" mce_href="?lang=en"><img xsrc="english_flag.gif" mce_src="english_flag.gif" /></a>

 

Now let me tell you how I allowed Smarty to place all existing GET variables into this equation; because if we don't put them, the localization of a page such as : http://grou.ps/login_recovery.php?lang=tr&email=iltates@gmail.com&serial=699786 becomes a mess; the link becomes http://grou.ps/login_recovery.php?lang=tr

OK here comes the solution: here is the actual code, just take a look at it, you'll understand:

<a xhref="?lang=en{foreach from=$smarty.get item=g key=k}&{$k}={$g}{/foreach}" style="border:0;"><img xsrc="images/flag_en.gif" mce_src="images/flag_en.gif" alt="EN" width="21" height="21" style="border:0" /></a

This part does the magic by running a simple Smarty foreach loop and leveraging Smarty's reserved variables:

{foreach from=$smarty.get item=g key=k}&{$k}={$g}{/foreach} 

Comments
VJ 3 months ago
Hi, Its really usefull for me.. thanks for this post. I wud expect more tip from u about smarty...Thanks...
VJ
marlowrence 87 days ago
My gratitude as well!
marlowrence



or
Connect with Facebook
Destekliyoruz...
Twitter
More about me...
My Flickr Stream
Notifications