Forum Moderators: coopster

Message Too Old, No Replies

Manipulating 'href' value in a link

Build a function to change the url...

         

mipapage

8:09 am on Aug 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I Have a 15-20 page site that will be built up in three languages. At the top of each page is a link to the equivalent page in the other languages.

I would like to write a function so that the value for href in the language links is created from the URL of the page and the respective language 'directory':

URL = [mysite.com...]
(rewritten from /template.php?page=somepage)

Language Link on Page =
<a href="en/somepage.htm">English</a>

The idea being that the php function 'writes' the 'en/somepage.htm' based on the url of the page.

For the link to the other language pages I could use:
function url_en ()
function url_de ()
function url_es ()

I've looked at explode/implode and parse_url, but am not sure that I am on the right track - i.e. is there a 'right' way to do this?

jamie

8:30 am on Aug 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi mipapage,

if you explode the $PATH_INFO into an array, which in your case is /en/somepage.htm

$array = explode("/",$PATH_INFO);

you get:
$array[1] = 'en'
$array[2] = 'somepage.htm'

($array[0] is always empty - i.e. the content to the left of the first slash)

you could then write a switch clause depending on the content of $array[1] to display the appropriate page

switch ($array[1]) {
case 'en':
load the english page;
break;

case 'de':
load the german page;
break;

case 'es':
load the spanish page
break;
}

then replace the 'load the page sentence' with the instructions you need to load the appropriate page.

that's if i've understood you correctly ;-)

good luck

mipapage

9:00 am on Aug 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jamie,

What you wrote is very cool, I like it, and I've learnt from it; however, I'm not sure it will work for me, let me try and explain, 'cause maybe I've misunderstood something!

What I would like is a function that automatically takes the url of the page and ads in the appopriate extra directory before the somepage.htm. So the code may look like this:

<div id="extras"> 
<a href="<? php function es()?>" title="Espanol">Espanol</a>&nbsp;¦&nbsp;
<a href="<? php function de()?>" title="Deutsh">Deutsh</a></div>

That code would reside in my template file, so that with any include it would automatically generate the correct URL when it sends out the page.

I'm wondering, could it be this then...


function de() {
$array = explode("/",$PATH_INFO);
echo "de/$array[2]";
}

The only drawback would be that the main site is spanish and resides at the root level, so there is no /es/. It could therefore be:

function es() { 
$array = explode("/",$PATH_INFO);
echo "/$array[1];
}

Not sure if I have that all right. Is it okay to use 'echo' there, or is that bad practise?
I really need to get out and study PHP some more.

mipapage

10:59 am on Aug 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay,

I've tried the above, and all I get for output are the "/" and the "de/". So the $array doesn't seem to want to print, or echo.

I've tried assigning $array[2] = $avariable, among other things, but no luck.

I'm sure this is a rookie thing. I googled and no luck. Any advice?

mipapage

11:09 am on Aug 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ignore this whole thread!

Sometimes I am just soooo... Well, it is Friday.

Ugh. Answer is so simple.

URL = www.mysite.com/template.php?page=somepage
Therefore
<a href="es/<? print "$page"?>" title="Espanol">Espanol</a>

This after two cups of coffee....

jamie

3:33 pm on Aug 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i'm still a bit confused, but glad you got it sorted - i have had many similar experiences ;-)