5 Replies - 1801 Views - Last Post: 08 March 2007 - 10:14 PM Rate Topic: -----

#1 gadgetsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 07-October 06

From the $_GET variable

Post icon  Posted 11 February 2007 - 02:06 PM

I've been trying to create a PHP document that uses the $_GET variable to get information from the URL. The purpose of doing this is so that I can translate this single page into multiple languages and use one page for different purposes such as reading an article or making changes to that article.
What I have so far is something like this:

mysite.com/page.php?en=true

or I can do this...

mysite.com/page.php?en=1

What I'm trying to do is make it so that the URL does not need the =true or =1
I want it to look just like this:

mysite.com/page.php?en
for the English version or:

mysite.com/page.php?admin
for the administration page or whatever I feel is necessary after the ?

The code that I have now is this:
<?php
function writePageLanguage() {
 $de = '(deutsch)';
 $en = '(English)';
 $es = '(español)';
 $fr = '(français)';
 $it = '(italiano)';

if ($_GET["de"] == true) {
  echo $de;
  }
elseif ($_GET["en"] == true) {
  echo $en;
  }
elseif ($_GET["es"] == true) {
  echo $es;
  }
elseif ($_GET["fr"] == true) {
  echo $fr;
  }
elseif ($_GET["it"] == true) {
  echo $it;
  }
else {
  echo $en;
  }
}
?>

The above code is function I'm planning on using inside the HTML title tag:
<title>My website <?php writePageLanguage(); ?></title>


I'm stuck on what to do next!

Is This A Good Question/Topic? 0
  • +

Replies To: From the $_GET variable

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 64
  • View blog
  • Posts: 3,505
  • Joined: 31-March 03

Re: From the $_GET variable

Posted 11 February 2007 - 03:46 PM

You'll want to use isset().

== is a typeless comparison. Meaning that you can equate the integer 1 with the string "1" or even any string with a boolean TRUE.
Was This Post Helpful? 0
  • +
  • -

#3 apollyonus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 07

Re: From the $_GET variable

Posted 13 February 2007 - 09:36 PM

View Postgadgetsguru, on 11 Feb, 2007 - 02:06 PM, said:

What I'm trying to do is make it so that the URL does not need the =true or =1
I want it to look just like this:

mysite.com/page.php?en


?en in the url defines $_GET['en'] in your code. If you want your ?en and $_GET['en'] variable to have a value, you have assign it one. Saying you want to use mysite.com/index.php?en is the same thing as trying to use $_GET['en'] = '';

I would suggest using a select box for your visitor to choose a language, or if you're after something a little more automatic, you can use a script to detect the default language on your visitor's box

hope this helps
Was This Post Helpful? 0
  • +
  • -

#4 Spider  Icon User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: From the $_GET variable

Posted 18 February 2007 - 12:00 PM

A more standard approach would be to set the language with

mysite.com/index.php?lang=en

or some other appropriately named variable.
Was This Post Helpful? 0
  • +
  • -

#5 gadgetsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 07-October 06

Re: From the $_GET variable

Posted 08 March 2007 - 04:46 PM

View PostSpider, on 18 Feb, 2007 - 12:00 PM, said:

A more standard approach would be to set the language with

mysite.com/index.php?lang=en

or some other appropriately named variable.


Oh, that's a really good idea! I didn't even think of that.
Thank you! :^:
Was This Post Helpful? 0
  • +
  • -

#6 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: From the $_GET variable

Posted 08 March 2007 - 10:14 PM

You can also put the languages into an array with defined keys, set the $_GET['lang'] as a variable that checks if the selected one exists as a key in your array (or set it to default), then echo the string from the array based on the selected key.

(if examples like this aren't allowed here, please tell me!)
which would shorten up your function to something like this:
<?php
function writePageLanguage()
{
$languages = array(
  'de' => '(deutsch)',
  'en' => '(english)',
  'es' => '(español)',
  'fr' => '(français)',
  'it' => '(italiano)'
);

$lang = (array_key_exists($_GET['lang'], $languages)) ? $_GET['lang'] : 'en';

echo $languages[$lang];
}
?>


Then you could use something like 'p' for pages, such as index.php?p=admin&lang=en
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1