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.

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

1 Answer 1

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 ;)

Sign up to request clarification or add additional context in comments.

3 Comments

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 :)
$w[$word]["it"] = $it;
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.