Log in

View Full Version : PHP $_GET help please (a bit of a newbie question)


steadicamop
07-26-2007, 10:22 AM
Hey guys, I'm just getting into PHP and have started on a site for someone, it will be dual language, and this is what I have so far:

<?php
if(isset($_GET['lang']))
if($lang == "en") { ?>
<div align="center">
<p align="justify">English Text</p>
</div> </div>
<?php
}
else if($lang == "nl")
{ ?>
<div align="center">
<p align="justify">Dutch Text</p>
</div></div>
<?php } ?>

Now, I'm using index.php?lang=en or lang=nl for each one, but I can figure out where I'm going wrong - when the page loads there is no text, I want it to default to Dutch - how do I achieve this? I thought I knew how but it wouldn't work.

Is there any other easier way of achieveing this as well? I thought this was the easiest way I could find.

Cheers

Jason

nexialys
07-26-2007, 10:40 AM
actually, $_GET['lang'] goes nowhere, as you call for a different variable afterward: $lang...

use $_GET['lang'] everywhere...

steadicamop
07-26-2007, 10:44 AM
Strangely enough - when I go down that road - it no longer works, but as my code above - that works fine ... any suggestions?

Cheers

[edit] I removed the == and replaced with just one - I've put two in and it's fine now, but I still need to figure out how to default it to Dutch (when there is no ?lang=nl placed at the end of the script)

Andreas
07-26-2007, 10:51 AM
<?php
if (!isset($_GET'lang']))
{
$lang = 'nl';
}
else
{
$lang =& $_GET['lang'];
}

if ($lang == 'en')
{
?>
<div align="center">
<p align="justify">English Text</p>
</div>
<?php
}
else
{
?>
<div align="center">
<p align="justify">Dutch Text</p>
</div>
<?php
}
?>


This defaults to nl if lang is not set or anything other than en.
Anyway, mixing code and data is no good practice!

I'd suggest to use a template engine.

steadicamop
07-26-2007, 10:57 AM
I'd suggest to use a template engine.

Store everything in a MySQL database and pull it out that way? That was an option but for a simple site, I thought it was just easier to pull it out of each page (plus it would share my Forum database which often has too many connections).

Thanks for your help - I understand it better now.

Jason

Andreas
07-26-2007, 11:03 AM
Store everything in a MySQL database and pull it out that way?
Not necessarily.

You might want to take a look at Smarty (http://smarty.php.net/).

steadicamop
07-26-2007, 11:46 AM
Installed and experimenting - thanks guys, your help is very much appreciated :)

Jason