Jump to content

MySQL how to get a value in a database based on $_SESSION value


gasma1975

Recommended Posts

Hi,

 

I'm doing a Multi-Language website, on my page I have a drop down list with English or French. When you select English the $_SESSION['lang'] = 1 and French $_SESSION['lang']=2

 

All of my translated sentences are stored in a database. The table name is 'lang'

 

'lang' has 4 fields

field 1 - ID -­> INT -­> Primary Key

field 2 -  ValueID -­> VARCHAR(100) -­> This is my identifier, corresponding to sentence that need to be translated

field 3 - L1 -­> TEXT -> english corresponding sentence

field 4 - L2 -­> TEXT -> French corresponding sentence

 

I would like to create a function, where I passe the ValueID and the value of $_SESSION['lang']

then it will go get the sentence in the specified language for the ValueID

 

I tried this:

 

function GetSentence($ValueID)

{

  $query="SELECT L$_SESSION['lang'] as Lsentence FROM lang WHERE id='$ValueID'";

  $result=mysql_query($query);

  $line=mysql_fetch_object($result);

  $Lsentence=$line->Lsentence;

  return $Lsentence;

}

 

not working.... any suggestion ?

 

gasma1975

Link to comment
Share on other sites

<?php

$langs = 2; // Increase this if you get more languages
$default_lang = 1; // Default language

function GetSentence($ValueID) {
    global $langs, $default_lang;
    
    $_SESSION['lang'] = (int)$_SESSION['lang'];
    if( $_SESSION['lang'] > $langs || $_SESSION['lang'] < 0 )
        $_SESSION['lang'] = $default_lang;
    
    $ValueID = (int)$ValueID;
    $query="SELECT L{$_SESSION['lang']} as Lsentence FROM lang WHERE id='$ValueID'";
    $result=mysql_query($query);

    if( !$result )
        return False;

    $line=mysql_fetch_object($result);
    return $line->Lsentence;
}

// ... later on

$sentence = GetSentence(10);

if( $sentence !== False ) {
// No error, continue
}
?>

 

Untested, give it a try

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.