2

following problem: I want to build a mysql based language system.

To decrease loading time and increase performance I thought it would be the best, to first fetch all Language phrases into an array (which happens with mysqli fetch array). Then, if a language term is required, calling a function with the array as global variable.

My function for the array:

$sql       = $db->query('SELECT * FROM lang_phrases WHERE phraseLanguageId = 1');
$phrases   = array();

while($row = $sql->fetch_array()) {
    $phrases[] = $row;
}

Now I've got this array:

array(2) { 
    [0]=> array(8) { 
        [0]=> string(1) "1" 
        ["phraseId"]=> string(1) "1"
        [1]=> string(1) "1" 
        ["phraseLanguageId"]=> string(1) "1" 
        [2]=> string(4) "HOME" 
        ["phraseKey"]=> string(4) "HOME" 
        [3]=> string(10) "Homepage" 
        ["phraseContent"]=> string(10) "Homepage" 
    } 
    [1]=> array(8) { 
        [0]=> string(1) "2" 
        ["phraseId"]=> string(1) "2" 
        [1]=> string(1) "1" 
        ["phraseLanguageId"]=> string(1) "1" 
        [2]=> string(4) "BACK" 
        ["phraseKey"]=> string(4) "BACK" 
        [3]=> string(6) "Back" 
        ["phraseContent"]=> string(6) "Back" 
    } 
}

And thats my function:

function l($key) {
    global $phrases;

    return PLACEHOLDER;
}

I wonder, how can I now search the array for "phraseKey" $key from the function and get the value? Do you have any hints? Or shall I simply select each phrase with each one query (performance problems with 100 quers per load?!)?

Cheers and Thanks!

1
  • Just as an aside, this will probably have a much lower overhead to do the same thing, if you've got enough control over your server to use it (probably want a dedicated server as you'll need to soft restart if/when you change the PO files) : php.net/manual/en/book.gettext.php Commented Jan 6, 2015 at 16:00

2 Answers 2

3

That's a bad structure. Why not key your array with the phrase ID and the language? e.g.

$translations = array(
    'HOME' => array(
       'english' => 'Home',
       'french' => 'Maison',
       'german' => 'Haus"
    etc...

This eliminates the need for ANY searching. You just look up directly by language/phrase ID.

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

2 Comments

I don't like the idea that you area loading all of the languages into this array
6 of this, half-a-dozen of the other. depends entirely on just how many phrases/languages OP has. at some point the overhead of buildnig this array will outweigh the overhead of only having parts of it and doing repeated queries.
2

May I suggest to retrive the phrases with

while($row = $sql->fetch_array()) {
    $phrases[$row['phraseKey']] = $row;
}

Then in your function l($key) you can search them with

$t = isset($phrases[$key]) ? $phrases[$key] : null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.