Translate using array_slice!?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Translate using array_slice!?

Post by jmansa »

I have a page where I have all the language saved in a mysql db. Now I want to create a script so I can translate the page to different languages in a easy way. Everything written on my page is generated by language snippets like this:

Code: Select all

<? echo ''._TOOLBAR_ADDLINK_TEXT_HEADER.''; ?>
It reffers to a page with the refering line like this:

Code: Select all

define("_TOOLBAR_ADDLINK_TEXT_HEADER","Welcome to the ToolBar");
This page is generated from mysql like this:

Code: Select all

$sql = mysql_query("SELECT * FROM ".$prefix."_lang_code WHERE lang_id = '1'");
while($row = mysql_fetch_array($sql)) {
                define("_".$row['tagname']."","".$row['string']."");
}
Now what I want is an easy way to translate this, and I thourgt I would do it like this:

Code: Select all

$langs = array();
$langs[2] = array();
$sql = mysql_query("SELECT * FROM ".$prefix."_lang_code WHERE lang_id = '1'");
while($row = mysql_fetch_array($sql)) {
    $langs[$row['lang_id']][] = $row['tagname'];
}
 
$sql = mysql_query("SELECT * FROM ".$prefix."_lang_code WHERE lang_id = '2'");
while($row = mysql_fetch_array($sql)) {
    if(!in_array($row['tagname'], $langs['2'])) {
        $edit[] = $row;
    }
}
 
echo "Still needs to be translated: ".count($edit);
 
$slice = array_slice($edit, 0, 1);
Now this should give the benefit, that if new tagnames is added I would be able to see it in this script and then translate, but...

I keep getting this error:

Warning: array_slice() expects parameter 1 to be array, null given in /home/mypage/public_html/admin/translate.php on line 71

I can't figure out where it goes wrong.

I have these fields in my DB:

id
lang_id
tagname
string

Can somebody please help!!!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Translate using array_slice!?

Post by josh »

well that variable is obviously never being initialized, if code ever hit that loop the array would have been created. NULL != an empty array

also I'd use a translation function or class, instead of polluting my global variable scope

something like

Code: Select all

 
echo translate( _HELP_TEXT );
 
the constant would just be a key value for that language value, the translate function use that key value to lookup the actual language to output and return it.
Post Reply