Search code examples
phpselecthttp-accept-language

How to initiate query if browser language does not match database tables, which named as countries?


I am not experienced in PHP, I know only basics. I don't know if I am asking this correctly, and I don't even know if this is possible, but I will try to explain.

  1. I have database with 10+ tables. Each table is named as country (Austria, Germany, Latvia, Lithuania..etc).

  2. I have script, which displays users browser language when he connects to website.

  3. Also, I have PHP query script, which displays data by users browser language (if user connects with browser, which primary language is Austrian, than PHP query display data from Austria table).

HTTP_ACCEPT_LANGUAGE

if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){ 
    $lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); 
}
if($lc == "de") { $langas = "Germany"; }
if($lc == "lt") { $langas = "Lithuania"; }
// etc..
echo $langas;

Query:

foreach($db->query('SELECT * FROM '.$langas.'') as $row) {
    echo '<span>'.$row['info1'].'</span>';
    echo '<span>'.$row['info2'].'</span>';
// etc..
}

What i want to do:

I want that if user connects to website, and his browser language does not match with my tables, than as default, query should initiate with other table and not send errors (for example Latvia table). How I can do that?

Other question:

I know, that if I will use HTTP_ACCEPT_LANGUAGE function, than I will be not able to use <select> code, that user would be able to manually select country. Maybe I am wrong.. maybe someone can explain that for me?


Solution

  • Try something like this

    $langas = '';
    
    if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){ 
        $lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); 
    
        if($lc == "de") { $langas = "Germany"; }
        if($lc == "lt") { $langas = "Lithuania"; }
        // etc
    } 
    
    if ( $langas == '' ) {
        // set default language
        $langas = "Germany";
    }
    
    foreach($db->query('SELECT * FROM '.$langas.'') as $row) {
        echo '<span>'.$row['info1'].'</span>';
        echo '<span>'.$row['info2'].'</span>';
    // etc..
    }
    

    Your second question needs to be asked as a seperate question!