Diline javascript / php

0 Cevap php

Ben yanlış bir şey yaptığımı biliyorum, ama ben ne yapıyorum yanlış anlamaya olamaz.

Web siteme dil geçiş basit bir seçme kutusunu kullanın. Ben select kutusuna bu işlevi bağlantılı:

function changeLang(){
    var chosenLang = $('#chooseLang').val();

    if(chosenLang != 0){
        window.location = "index.php?lang="+chosenLang
        }
}

Language.php dosyasına ben şu kodu kullanın:

<?php
$lang = $_GET['lang'];
$myLang = $_COOKIE["myLang"];

// one month to expire
$expire = time()+60*60*24*30;

if (!isset($_COOKIE["myLang"])){
    setcookie("myLang", "en", $expire);
    include "languages/en.php";
    }else{
    include "languages/$myLang.php";
}



if($lang == "en"){
    include "languages/en.php";
    setcookie("myLang", "en", $expire);
    }else if($lang == "fr"){
        include "languages/fr.php";
        setcookie("myLang", "fr", $expire);
        }else if($lang == "nl"){
            include "languages/nl.php";
            setcookie("myLang", "nl", $expire);
}

?>

Everything works fine, the cookies value is changed to the new language successfully every time. All the variables are also changing. The only problem is the queries I use to retrieve data from the database in the chosen language. It seems like it's always a step behind. Example: If I start the website, the language is in English. Once I switch to French, all variables are immediately changed to the French language, but the rows from the queries are still in English. Only when I manually refresh the page, the rows change to the desired language.

The data in the database is entered like this: language_id categories

Example: language_id = 1 (English) categories = Singer

language_id = 2 (French) categories = Chanteur

language_id = 3 (Dutch) categories = Zanger

Ben verileri almak için aşağıdaki kodu kullanabilirsiniz:

if($myLang == "en" || !$myLang){
$lang = 1;                      
}else if($myLang == "fr"){
$lang = 2;
}else if($myLang == "nl"){                                
$lang = 3;                          
}

$stmt = $dbh->prepare("SELECT cat_title, cat_group
            FROM categories
            WHERE cat_lang_id = '$lang'
            ORDER BY  category_id ASC");
/*** execute the prepared statement ***/
$stmt->execute();
/*** fetch the results ***/
$result = $stmt->fetchAll();

Ben ancak herhangi bir şans olmadan, ben bu çözmek için biliyorum her şeyi denedim.

Şimdiden teşekkürler.

0 Cevap