anatak Posted October 28, 2006 Share Posted October 28, 2006 Hello,I am trying to make content available in multiple languages.content_info table holds all the data about one item except the textcontent_text table holds the text about one itemthe relationship is one (content_info) to many (content_text) as the text can be available in multiple languages.I want to setup the site that the text is displayed in the prefered language of the user if it is available in that language.If the text is not available in that language it should be displayed in the default language.[code]# Table structure for table `content_info`#CREATE TABLE `content_info` ( `Id` int(11) NOT NULL auto_increment, `Author` int(11) default NULL, `Type` int(11) default NULL, `UpdateDate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `PostDate` timestamp NOT NULL default '0000-00-00 00:00:00', `Picture` int(11) default NULL, `Place` int(11) default NULL, `Date` date default '0000-00-00', `Price` int(11) default NULL, PRIMARY KEY (`Id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;# Table structure for table `content_text`#CREATE TABLE `content_text` ( `Id` int(11) NOT NULL auto_increment, `LanguageId` int(11) default NULL, `Translator` int(11) default NULL, `InfoId` int(11) default NULL, `Title` varchar(255) default NULL, `Subtitle` varchar(255) default NULL, `Text` text, PRIMARY KEY (`Id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;[/code]The way I have it now is like this.[code]SELECT content_info.Id FROM content_info;[/code]and then loop through all the Id's.[code]foreach($result01 AS $row01){ echo "<br />ID: ".$row01['Id']; //query to select the default language (english) $Query02="SELECT $TableName01.Id, $TableName02.Title, $TableName02.Subtitle FROM $TableName01, $TableName02 WHERE $TableName01.Id = $row01[Id] AND $TableName02.InfoId = $row01[Id] AND $TableName02.LanguageId = 1 ;"; //query to select the userdefined language $Query03="SELECT $TableName01.Id, $TableName02.Title, $TableName02.Subtitle FROM $TableName01 JOIN $TableName02 ON $TableName01.Id = $TableName02.InfoId WHERE $TableName01.Id = $row01[Id] ;"; echo $Query02."<br />"; echo $Query03."<br />"; }[/code]After I have the results I can check in php if a the text fields of $Query03 are empty and if they are I display the texts fields from $Query02The problem is that just for listing the title and subtitle of the content I have to run 2 queries for every item.Can anyone think about a more performant way to do this or do you think this is not a problem for the DB.Or can you think about a better DB design ?thank youanatak Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/ Share on other sites More sharing options...
fenway Posted October 28, 2006 Share Posted October 28, 2006 Well, you could use a derived table to pull the desired language "first", and then JOIN it back to the parent table. Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/#findComment-115892 Share on other sites More sharing options...
anatak Posted October 29, 2006 Author Share Posted October 29, 2006 What I need is a left join with a where clause :(SELECT content_info.*, content_text.* FROM content_info LEFT JOIN content_text ON content_info.Id = content_text.InfoIdwill give me all the records from content_info and the records from content_textThe good thing is that it also returns the content_info records if there is not matching content_text.Now I would like to be able to getall the records from content_info that match content_text where the language of context_text is set AND all the records from content_info that have no match with content_textI am not sure if it is possible in SQL Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/#findComment-116116 Share on other sites More sharing options...
fenway Posted October 29, 2006 Share Posted October 29, 2006 With a UNION? Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/#findComment-116404 Share on other sites More sharing options...
anatak Posted October 30, 2006 Author Share Posted October 30, 2006 A union would not work.but I found a different approach.I am going to run two queriesone query to select all the rows that have an english textone query to select all the rows that have the desired language textafter that I have to check if the same id exists in both arrays that hold the results of the queries.if the Id exists in both arrays I have to print the text in the desired language if the id only exists in the first array I have to print the text in english.of course easier said than done.do you know if you can manipulate a $result = mysql($query) as any array ?anatak Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/#findComment-116577 Share on other sites More sharing options...
shoz Posted October 30, 2006 Share Posted October 30, 2006 You should be able to use the following query to get the desired language and the default[code]SELECTt1.title AS dsrd_title, t2.title AS dflt_titleFROMcontent_info iLEFT JOINcontent_text AS t1ONt1.info_id=i.id AND t1.lang_id = $userDefLangINNER JOINcontent_text AS t2ONt2.info_id = i.id AND t2.lang_id = 1[/code][code]<?phpwhile ($row = mysql_fet...){ $title = isset($row['dsrd_title'])? $row['dsrd_title']: $row['dflt_title'];}?>[/code]You should also put a multicolumn index on (info_id, lang_id);EDIT: Made LEFT JOIN t2 an INNER JOIN, meaning that there has to be text in the default language (lang_id = 1) or the query will not return any results for that article. Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/#findComment-116665 Share on other sites More sharing options...
fenway Posted October 30, 2006 Share Posted October 30, 2006 [quote author=anatak link=topic=112993.msg459805#msg459805 date=1162186006]A union would not work.but I found a different approach.[/quote]I thought you mean "AND" in the english sense, not the database sense. Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/#findComment-116827 Share on other sites More sharing options...
anatak Posted October 31, 2006 Author Share Posted October 31, 2006 Hey shoz,What can I say ?Pure genius. Thanks a lot.anatak Quote Link to comment https://forums.phpfreaks.com/topic/25364-how-to-check-if-field-has-a-value/#findComment-117184 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.