Link to home
Start Free TrialLog in
Avatar of SSupreme
SSupremeFlag for Belarus

asked on

PHP link formation or write in html

Here I got my current link builder:
foreach ($langs as $lang_code => $lang_name)
{
    $out[] = "<a href=\"{$_SERVER["PHP_SELF"]}?lang=$lang_code\">$lang_name</a>";
}
Here is output: <a href="/test/langui.php?lang=es">Spanish</a>
so it redirects to: mysite.com/test/langui.php?lang=es
I want it build link so it redirects to: mysite.com/test/es/langui.php
Output should be: <a href="/test/es/langui.php">Spanish</a>

Easy points here.
Avatar of saimazz
saimazz

foreach ($langs as $lang_code => $lang_name)
{
    $out[] = "<a href=\"{$_SERVER["SERVER_NAME"]}/{$lang_code}/{$_SERVER["SCRIPT_NAME"]}\">$lang_name</a>";
}
some corrects:

foreach ($langs as $lang_code => $lang_name)
{
    $out[] = "<a href=\"{$_SERVER["SERVER_NAME"]}/{$lang_code}/{$_SERVER["SCRIPT_FILENAME"]}\">$lang_name</a>";
}

Open in new window

i didnt realize before, that you had folder in url, now should work fine:

foreach ($langs as $lang_code => $lang_name)
{
$temp = explode('/',$_SERVER["SCRIPT_NAME"]);
$temp[] = end($temp);
$temp[sizeof($temp) - 2] = $lang_code;
$link = implode('/', $temp);
$out[] = "<a href=\"$link\">$lang_name</a>";
}

Open in new window

Avatar of SSupreme

ASKER

I asked this question so I don't want to check it for errors each time and someone can answer it straight away.
You current version gives me this:
http://mysite.com/test/mysite.com/es//usr/web2/test/langui.php 

Please check you answer before posting here.
foreach ($langs as $lang_code => $lang_name)
{
    $out[] = '<a href="'.
	 pathinfo($_SERVER['PHP_SELF'],PATHINFO_DIRNAME) .'/'.
	 $lang_code .'/'.
	 pathinfo($_SERVER['PHP_SELF'],PATHINF_BASENAME) .'">'.
	 $lang_name .'</a>';
}

Open in new window


HTH
Derokorian, output is: http://mysite.com/test/es/
saimazz, output is Correct: http://mysite.com/test/es/langui.php 
Could you please simplify it.
OMG sorry I made a typo:

foreach ($langs as $lang_code => $lang_name)
{
    $out[] = '<a href="'.
         pathinfo($_SERVER['PHP_SELF'],PATHINFO_DIRNAME) .'/'.
         $lang_code .'/'.
         pathinfo($_SERVER['PHP_SELF'],PATHINFO_BASENAME) .'">'.
         $lang_name .'</a>';
}

Open in new window

In statements like this one:

$temp = explode('/',$_SERVER["SCRIPT_NAME"]);

The intent of the slash is to be used as a directory separator.  PHP has a context-aware constant DIRECTORY_SEPARATOR that should be used instead of the quoted slash.  It will make your script portable between operating systems if you use the PHP constant.
SOLUTION
Avatar of saimazz
saimazz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops... Sorry - I left the GET processing in the script from your other question.  But in any case the code from line 35 to the end generates the URLs in the format you want.
Actually Ray, you would get es/test/langui.php as opposed to /test/es/langui.php
Obviously, /test/ is mean test, so in reality I will use it in / anyway. Based on this I can say that Derokorian's and saimazz's solutions work only in /test/. On other hand Ray_Paseur's solution is works just right in /test/ output is mysite.com/test/es/test/langui.php and in / output is mysite.com/pl/langui.php
what is correct.
I've requested that this question be closed as follows:

Accepted answer: 300 points for Ray_Paseur's comment http:/Q_27405007.html#36994717
Assisted answer: 100 points for Derokorian's comment http:/Q_27405007.html#36994674
Assisted answer: 100 points for saimazz's comment http:/Q_27405007.html#36994698
Assisted answer: 0 points for SSupreme's comment http:/Q_27405007.html#36994850

for the following reason:

Thanks
First off, if you tried my solution you'd notice it would work in ANY directory including the root directory.
Well, in any case, it's just a matter of moving the correct strings into the correct positions in the URL, but without understanding the site's directory structure we are all a bit at a loss to provide the exact answer.  The php.net web site has it figured out.

http://us2.php.net/manual/en/function.urlencode.php
http://us2.php.net/manual/es/function.urlencode.php
http://us2.php.net/manual/de/function.urlencode.php
First off, if you tried my solution you'd notice it would work in ANY directory including the root directory.
Derokorian, watch my first even screencast.
SSupreme-513882.flv
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
want to close differently