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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.