2

I am not an expert in PHP/MySql so my first idea about how to handle a website with multiple languages and making the PHP code readable was the following:

1) Creating a table with "word, en, it, de, fr" fields, one for each string I want to be translated. For example:

 WORD        EN        IT        DE        FR
 title       HomePage  Pagina              No Idea
 thanks      Thank You Grazie    Danke     Merci

2) Storing the table in an array of array of strings, so that I could access it in this way:

 print $word["thanks"]["it"];

which IMHO would make the PHP code very readable (performance is NOT IMPORTANT).

So, I wrote this code to read the data from the DB, and if the translation is empty, I fill the array with "the word in XX is missing" (to avoid pasting all the identical code, I am only showing you one case):

 $s = "SELECT word, it, en, de, fr FROM languages";
 $result = $conn->query($s);
 while ($r = $result->fetch_array(MYSQLI_ASSOC)) {
   $word = $r["word"];
   $it = $r["it"];
   // and DE and EN and FR...
   if ($it != "")
     $w["$word"]["$it"] = $it;
   else
     $w["$word"]["$it"] = $word." IT missing";
   // and DE and EN and FR...
 }   

I tried putting the double quotes, I tried removing them... But every time I print the $w variable, is ALWAYS empty. The intermediate variables read from the DB look ok.

What am I doing wrong? Thank you.

  • So when you print it? Right after while loop? – u_mulder Aug 2 '15 at 17:00
  • Yes. And even printing directly a single element (like in the example provided) does not do anything :( – ZioBit Aug 2 '15 at 17:03
  • 1
    you definitely don't need quotes around "$word" and "$it". – Jessica Aug 2 '15 at 17:03
  • And if var_dump($r) -what it shows? – u_mulder Aug 2 '15 at 17:04
  • add var_dump($r) after while ($r = $result->fetch_array(MYSQLI_ASSOC)) { - what do you see? – Jessica Aug 2 '15 at 17:04
3

Test this.

<?php
$s = "SELECT word, it, en, de, fr FROM languages";
$result = $conn->query($s);
$languages = array('it', 'en', 'de', 'fr');
while ($r = $result->fetch_array(MYSQLI_ASSOC)) {
    foreach ($languages as $lang) {
        $word = strval(trim($r["word"]));
        $translated_word = strval(trim($r[$lang]));
        (strlen($translated_word) > 0) ? $w[$word][$lang] = $translated_word : 
                $w[$word][$lang] = $word . " " . strtoupper($lang) . " missing";
    }
}   

ZioBit edit: Dummy text because SO does not let me modify a single character ;)

| improve this answer | |
  • Thank you :) I just removed an extra ";" (after the assign on the first case of the "?") and it works... But still I do not know what was wrong with my code :) – ZioBit Aug 2 '15 at 17:52
  • $w[$word]["it"] = $it; – koredalin Aug 2 '15 at 17:56
  • Ahahah :) I should stop coding after midnight. I should stop coding after midnight. I should stop coding after midnight. Nobody else noticed it :) Thanks again. – ZioBit Aug 2 '15 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.