Page 1 of 1

URL Queries

Posted: Wed Jul 15, 2009 8:48 am
by Cencere
I recently added a second language to my site and have allowed visitors to switch between the two by clicking on the language in the menu. Upon clicking the link &lang=<language> is added to the base url and they are sent there. However the code allows for there to be two paramaters for the language because of the way I write the base url. I made a function to by-pass this but I was wondering if there was a more efficient way of doing it.

Problem Code:

Code: Select all

$uri = basename($_SERVER['PHP_SELF']) . '?'; //gets page name
foreach ( $_GET as $key=>$val ) { $uri .= $key . '=' . rawurlencode($val) . '&'; //appends all parameters in query to base url }
And the Link Code:

Code: Select all

<li><a class="menuItem" href="<?php echo $uri; ?>lang=english">English</a></li>
As you can see, the Link Code just assumes there isn't a language parameter set yet. So I'm wondering if it's possible to fix that.

Patch Function:

Code: Select all

function newlink( $editedKey, $editedVal) {
$_GET[$editedkey] = $editedVal;
$uri = basename($_SERVER['PHP_SELF']) . '?'; //gets page name
foreach ( $_GET as $key=>$val ) { $uri .= $key . '=' . rawurlencode($val) . '&'; 
return $uri;
}
The function returns the new URL with edited (not appended) parameters. I was just wondering if there was a better way to do this. Thanks!