greenday Posted January 22, 2008 Share Posted January 22, 2008 I have 1 table called article, and another called articlecategory. They are linked through a field called articlecategoryid. To display the results in the front end I am creating an SQL statement for each article category like: SELECT * from articles and articlecategory WHERE articlecategoryid both = international news and then another for the next articlecategory: SELECT * from articles and articlecategory WHERE articlecategoryid both = local news and so on. I know this is the wrong way to do it, and if an administrator adds a new articlecategory, then it obviously wont show up because each articlecategory is hand coded in. What is the correct way to do this, so it will cycle through each articlecategory and add the latest story underneath the title for each like this: Articlecategory Title 1 Latest story for articlecategory title 1 Articlecategory Title 2 Latest story for articlecategory title 2 Articlecategory Title 3 Latest story for articlecategory title 3 And so on.. Some articlecategory's may not have any related stories . Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/87168-title-and-content-from-2-tables/ Share on other sites More sharing options...
beebum Posted January 22, 2008 Share Posted January 22, 2008 http://www.informit.com/articles/article.aspx?p=102167&seqNum=3 Quote Link to comment https://forums.phpfreaks.com/topic/87168-title-and-content-from-2-tables/#findComment-446129 Share on other sites More sharing options...
fenway Posted January 23, 2008 Share Posted January 23, 2008 Sounds like you need to join the table... though your syntax with AND is incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/87168-title-and-content-from-2-tables/#findComment-447252 Share on other sites More sharing options...
greenday Posted January 23, 2008 Author Share Posted January 23, 2008 the 'and' was just written in plain english to get an idea of what the sql was like. I have joind the table, but cant work out how to do the correct join to get the results i need. Quote Link to comment https://forums.phpfreaks.com/topic/87168-title-and-content-from-2-tables/#findComment-447308 Share on other sites More sharing options...
teng84 Posted January 23, 2008 Share Posted January 23, 2008 heres ex..on how to join SELECT * from articles INNER JOIN articlecategory on articlecategory.articlecategoryid = articles.articlecategoryid WHERE both = international news syntax... select (columns) from table1 inner join table2 on table1.field = table2.fields where etc.. and etc.. hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/87168-title-and-content-from-2-tables/#findComment-447321 Share on other sites More sharing options...
greenday Posted January 23, 2008 Author Share Posted January 23, 2008 Thanks, thats great. However this will only give the results for 1 category i.e. International News. How can I cycle through all differant categories, with 1 news for each category like this: Articlecategory Title 1 Latest story for articlecategory title 1 Articlecategory Title 2 Latest story for articlecategory title 2 Articlecategory Title 3 Latest story for articlecategory title 3 Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/87168-title-and-content-from-2-tables/#findComment-447346 Share on other sites More sharing options...
fenway Posted January 24, 2008 Share Posted January 24, 2008 What's "both"? Why not just drop the where clause? Quote Link to comment https://forums.phpfreaks.com/topic/87168-title-and-content-from-2-tables/#findComment-447990 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.