![]() |
How to get categories for posts? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: How to get categories for posts? (/showthread.php?tid=51955) |
How to get categories for posts? - El Forum - 05-24-2012 [eluser]Unknown[/eluser] Hi. I have general page that contain 10 posts for each post can be 0 or many categories. How I can show categories for each post? Here my tables Code: CREATE TABLE IF NOT EXISTS `articles` ( I want show 10 posts on page. First I get 10 entries from table `articles`. In artciles entry category can have '1|2' - that id's table categories. Can you show me example function that can get all categories for each post. I don't want use foreach for every post. Couse I think it very slow way. How to get categories for posts? - El Forum - 05-24-2012 [eluser]luismartin[/eluser] This question would fit much more in a SQL forum. Anyway I see something that could be wrong: the foreign key in table 'articles' for table 'categories'. You seem to be using a varchar which is not an index in 'categories' to join them. Also, if that's a many-to-many relationship, as it seems to be (an article has 0, 1 or more categories and a category has 0 to many articles), you need an intermediate table. Then having these three tables: articles, articles-categories, categories, you'll want to do a left join from articles to articles-categories, then an inner join from articles-categories to categories. |