...

View Full Version : I'm Stuck... choosing a variable depending on the value of another variable



neillglobal
03-30-2007, 07:43 AM
Hey Guys,

I'm stuck on this... I'm trying to use a function to select the right array row. So for instance if I type in :


langText (txt_userpanel_greeting, cmn)


I'll want it to read the mandarin row of the $txt_userpanel_greeting array...

Any clues?



// PAGE TEXT ARRAYS
$txt_userpanel_greeting['eng'] = "Hey ";
$txt_userpanel_greeting['cmn'] = "你好 ";
$txt_userpanel_greeting['tha'] = "สวัสดี ";
$txt_userpanel_greeting['kor'] = "안녕하세요 ";
// /PAGE TEXT ARRAYS

function langText($ftext, $flang)
{
echo $$ftext["$flang"];
}

langText(txt_userpanel_greeting, eng);


Thanks guys... I'm trying to make my website multilingual! :D

Acid
03-30-2007, 12:15 PM
Try this:


<?php

$txt_userpanel_greeting = array();
$txt_userpanel_greeting['eng'] = "Hey ";
$txt_userpanel_greeting['cmn'] = "你好 ";
$txt_userpanel_greeting['tha'] = "สวัสดี ";
$txt_userpanel_greeting['kor'] = "안녕하세요 ";

function langText($ftext, $flang) {

foreach ($ftext as $key => $value) {

if ($key == $flang) {
return $value;

}

}

}

echo langText($txt_userpanel_greeting, "eng");

?>

dumpfi
03-30-2007, 02:51 PM
Just do:
<?php
$txt_userpanel_greeting = array();
$txt_userpanel_greeting['eng'] = "Hey ";
$txt_userpanel_greeting['cmn'] = "你好 ";
$txt_userpanel_greeting['tha'] = "สวัสดี ";
$txt_userpanel_greeting['kor'] = "안녕하세요 ";

function langText($ftext, $flang)
{
if(!isset($ftext[$flang]))
{
//error
}
return $ftext[$flang];
}

echo langText($txt_userpanel_greeting, "eng");
?>dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum