barney0o0 Posted December 5, 2015 Share Posted December 5, 2015 Hi, i have a db table in this structure: langid | tx_en | tx_it | tx_ru 1 welcome benvenuto Добро пожаловат 2 contact Contatto Контактная etc, etc After the initial query of 'SELECT tx_".$lang." FROM langs' , is it possible to echo via php these values, however id or record number based? Such as <?php echo $row_langs['tx_'.$lang] (WHERE langid = 2); ?> or <?php echo $row_langs['tx_'.$lang] (WHERE langid = 1); ?> ? Throughout pages i have 10 - 20 different keywords and phrases stored in the above table that need to be echo throughout page. Rather than making 20 queries (to retrieve specifc id's) per page, i thought just make one query and echo the data after....is this actually possible or should i rethink? Thanks in advance Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 5, 2015 Share Posted December 5, 2015 for ease of use and code readability, you need to end up with the language strings either in defined constants or an array, with the defined constant name or the array index indicating the meaning of the string. you can dynamically produce the defined constants/array from wherever the data is actually stored at. for these two examples, the Italian language was selected. only the building of the defined constant/array is dependent on the language choice. the actual usage in the code is the same regardless of the language choice. if you use the defined constant method - // definition define('LANG_contact', 'Contatto'); // the define() statements can be dynamically called if the data is stored in a database table. // usage echo LANG_contact; if you use the array method - // definition $lang['contact'] = 'Contatto'; // the array can be build dynamically if the data is stored in a database table. // usage echo $lang['contact']; Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted December 5, 2015 Author Share Posted December 5, 2015 Thanks...so..... From an array perspective, would it be possible to create string based in id, i.e. $lang['2'] = 'Contatto'; echo $lang['2']; ..as i havent done one before, would the code by something like this? $array1 = array(); $result = mysql_query("SELECT langs.tex_".$lang.", langs.langid FROM langs"); while ($row = mysql_fetch_assoc($result)) { $lang=$row['tex_".$lang."']; $langs[] = $lang; } Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 5, 2015 Share Posted December 5, 2015 Your main problem right now is that your database schema is very poor. Cryptic IDs and language names buried in column names will turn this trivial task into a PITA. So before you do anything, I recommend you fix your database structure. For example: languages - code CHAR(2) PRIMARY KEY (a unique language code, e. g. the two-letter ISO 639-1 code) - name TEXT (e. g. “English”) items (the items to be translated) - id VARCHAR(100) PRIMARY KEY (a textual identifier of the item, e. g. “welcome”) translations - item VARCHAR(100) REFERENCES items (id) - language CHAR(2) REFERENCES languages (code) - translation TEXT - PRIMARY KEY (item, language) So the translations are stored as individual rows and can easily be accessed: item | language | translation --------+----------+---------------- welcome | en | welcome welcome | it | benvenuto welcome | ru | Добро пожаловат SELECT item, translation FROM translations WHERE language = :language_code 1 Quote Link to comment 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.