Translate using array_slice!?

Discussion in 'PHP' started by jmansa, Oct 5, 2008.

  1. #1
    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:

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

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

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

    $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);
    Code (markup):
    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!!!
     
    jmansa, Oct 5, 2008 IP
  2. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $edit is null in case nothing needs to be translated. So a $edit = array() before any if-statements.

    Also.. why would you run both SQL SELECT queries when you only need one? Choose which language to use first (based on subdomain/cookie/whatever), then do a switch/case to run only the necessary query which fills your array.
     
    keyaa, Oct 5, 2008 IP