jetsettt Posted April 15, 2011 Share Posted April 15, 2011 Hi, I am trying to build a multi-language function for my website. I have looked at many tutorials and all seem to suggest that language files are used. However, I wish to have easier administration of the language entries and storing these in a DB would be much better for myself. I have 2 tables. This is my schema. 'Lang' table 'Lang_id' 'Lang_name' (eg English) 'Lang_entries' table 'Lang_entry_id' 'Lang_entry_data' (eg. 'Hello! This is English text') 'Lang_entry_code' (eg. AA0001) 'Lang_id' (eg. '1') Not sure if this is the best way, but I intend to query the 'Lang_entries' table by 'Lang_id' for example and get all of the 'English' entries so that they can be PHP echoed in various places on the page by 'Lang_entry_code' The problem that I am having is that I can only echo the first result from the query. Is it possible to echo any of the 'Lang_entry_data' by a 'Lang_entry_code' ? Thankyou in advance if anyone can advise. Not sure how to go about this. Quote Link to comment https://forums.phpfreaks.com/topic/233846-multi-language-website-function/ Share on other sites More sharing options...
lastkarrde Posted April 15, 2011 Share Posted April 15, 2011 What is your query? Quote Link to comment https://forums.phpfreaks.com/topic/233846-multi-language-website-function/#findComment-1202155 Share on other sites More sharing options...
jetsettt Posted April 15, 2011 Author Share Posted April 15, 2011 This is the query that I am using to bring back all results for 'English' (id 1) SELECT lang_entry_data, lang_entry_code FROM lang_entries WHERE lang_id = '1' Quote Link to comment https://forums.phpfreaks.com/topic/233846-multi-language-website-function/#findComment-1202159 Share on other sites More sharing options...
jetsettt Posted April 15, 2011 Author Share Posted April 15, 2011 I have come up with a solution that works but I am unsure if it good practice. To display a language entry from the DB anywhere on the page, I run a DB query such as ... <?php $query_language = "SELECT lang_entry_data, lang_entry_code FROM lang_entries WHERE lang_entry_code = 'AA0005'"; $language = mysql_query($query_language, $db) or die(mysql_error()); $row_language = mysql_fetch_assoc($language); echo $row_language['lang_entry_data']; ?> It seems to work ok without having to load the whole of the language entries into the page for use. This will result in many DB quiries per page, is this bad practice ? Quote Link to comment https://forums.phpfreaks.com/topic/233846-multi-language-website-function/#findComment-1202179 Share on other sites More sharing options...
analog Posted April 15, 2011 Share Posted April 15, 2011 A lot of queries for a single page would be quite bad for performance and scalability. You can load all the language strings into an assoc array and use that. Something like this (Untested code): <?php $query_language = "SELECT lang_entry_data, lang_entry_code FROM lang_entries WHERE lang_id = '1'"; $language = mysql_query($query_language, $db) or die(mysql_error()); $lang = array(); while($row = mysql_fetch_assoc($language)) $lang[$row_language['lang_entry_data']] = $row_language['lang_entry_data']; echo $lang['AA0005']; // output the lang string for AA0005 ?> Quote Link to comment https://forums.phpfreaks.com/topic/233846-multi-language-website-function/#findComment-1202180 Share on other sites More sharing options...
jetsettt Posted April 15, 2011 Author Share Posted April 15, 2011 Thanks for the reply. I guessed that repeated queries throughout the page might make an impact on performance. The method of using an array looks like a better solution. I will give this a try. Thanks very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/233846-multi-language-website-function/#findComment-1202191 Share on other sites More sharing options...
jetsettt Posted April 16, 2011 Author Share Posted April 16, 2011 Thanks analog, Just what I need. This seems a really good way to add multi-language to a web site. Storing the data in a DB rather than files is much better for me. I had to make a small change to the code to get it to work so that the correct language entry data could be called by it's ID. All I have to do now to show a language entry anywhere on a page is call it by its ID! Thanks again analog. $lang[$row_language['lang_entry_data']] = $row_language['lang_entry_data']; Altered to. $lang[$row_language['lang_entry_code']] = $row_language['lang_entry_data']; Quote Link to comment https://forums.phpfreaks.com/topic/233846-multi-language-website-function/#findComment-1202295 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.