gasma1975 Posted January 6, 2008 Share Posted January 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84739-mysql-how-to-get-a-value-in-a-database-based-on-_session-value/ Share on other sites More sharing options...
rab Posted January 6, 2008 Share Posted January 6, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/84739-mysql-how-to-get-a-value-in-a-database-based-on-_session-value/#findComment-431846 Share on other sites More sharing options...
gasma1975 Posted January 6, 2008 Author Share Posted January 6, 2008 Works fine ! Thx, I love learning from the PROs ! Quote Link to comment https://forums.phpfreaks.com/topic/84739-mysql-how-to-get-a-value-in-a-database-based-on-_session-value/#findComment-431854 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.