• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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.
0
SSupreme
Asked:
SSupreme
  • 6
  • 5
  • 4
  • +1
3 Solutions
 
saimazzCommented:
foreach ($langs as $lang_code => $lang_name)
{
    $out[] = "<a href=\"{$_SERVER["SERVER_NAME"]}/{$lang_code}/{$_SERVER["SCRIPT_NAME"]}\">$lang_name</a>";
}
0
 
saimazzCommented:
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

0
 
saimazzCommented:
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SSupremeAuthor Commented:
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.
0
 
DerokorianCommented:
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
0
 
SSupremeAuthor Commented:
Derokorian, output is: http://mysite.com/test/es/
saimazz, output is Correct: http://mysite.com/test/es/langui.php 
Could you please simplify it.
0
 
DerokorianCommented:
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

0
 
Ray PaseurCommented:
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.
0
 
saimazzCommented:
foreach ($langs as $lang_code => $lang_name)
{
    $out[] = '<a href="'.
	 pathinfo($_SERVER['PHP_SELF'],PATHINFO_DIRNAME) .'/'.
	 $lang_code .'/'.
         pathinfo($_SERVER['PHP_SELF'],PATHINFO_FILENAME).
         '.'.
         pathinfo($_SERVER['PHP_SELF'],PATHINFO_EXTENSION).'">'.
	 $lang_name .'</a>';
}

Open in new window

0
 
Ray PaseurCommented:
Generated HTML:

<br/>THE SITE IS NOW IN <b>English</b><br/><a href="es/RAY_temp_ssupreme.php">Spanish</a> | <a href="pl/RAY_temp_ssupreme.php">Polski</a> | <a href="de/RAY_temp_ssupreme.php">Deutsch</a>
<?php // RAY_temp_ssupreme.php
error_reporting(E_ALL);

// Spanish version -> English | Polski | Deutsch

// SET THE ARRAY OF POSSIBLE LANGUAGES
$langs = array
( 'es' => 'Spanish'
, 'en' => 'English'
, 'pl' => 'Polski'
, 'de' => 'Deutsch'
)
;

// SET THE DEFAULT LANGUAGE
$lang     = 'en';
$language = $langs[$lang];

// IF THERE IS A GET ARGUMENT
if (isset($_GET["lang"]))
{
    if (array_key_exists($_GET["lang"], $langs))
    {
        $lang     = $_GET["lang"];
        $language = $langs[$lang];
    }
}

// WHAT LANGUAGE ARE WE USING?
echo "<br/>THE SITE IS NOW IN <b>$language</b><br/>";

// CREATE LINKS FOR THE LANGUAGES
// $out[] = $langs[$lang];
unset($langs[$lang]);
foreach ($langs as $lang_code => $lang_name)
{
    $out[] = '<a href="' . $lang_code . $_SERVER["PHP_SELF"] . '">' . "$lang_name</a>";
}
echo implode(' | ', $out);

Open in new window

0
 
Ray PaseurCommented:
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.
0
 
DerokorianCommented:
Actually Ray, you would get es/test/langui.php as opposed to /test/es/langui.php
0
 
SSupremeAuthor Commented:
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.
0
 
SSupremeAuthor Commented:
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
0
 
DerokorianCommented:
First off, if you tried my solution you'd notice it would work in ANY directory including the root directory.
0
 
Ray PaseurCommented:
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
0
 
SSupremeAuthor Commented:
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
0
 
DerokorianCommented:
Odd it works fine in base directory for me on my wamp localhost but you're right its not working for me on linux - however its different that how it "doesn't work" in your video in that it added an extra slash to the start, so I'm unsure how you got the result you have. Either way you can get around it with this:

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

Open in new window

0
 
SSupremeAuthor Commented:
want to close differently
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now