我有两个表,一个是博客新闻,另一个是文章。简单地说,我需要做的就是加入他们,这样我就有了这样的代码
$query = $this->db->select('*')
->from('blogs')
->select('blogs.id as BlogId....')
->join('articles', 'articles.highlight = 1', 'left')
->join('authors', 'articles.author = authors.id', 'left')
->join('companies', 'authors.company_id = companies.id')
->get();
Join工作正常,但我需要对foreach中的数据进行echo,我的问题来了。我在文章下面复制了博客。所以,如果我在每一篇is博客下都有5篇文章(我只有一篇)。有没有可能只显示一个博客然后显示文章?
感谢
大多数人会告诉你在外表上foreach,但这太可怕了。尝试使用Undercore库http://brianhaveri.github.com/Underscore.php/#groupBy在你的结果数组上,正如我在这里展示的:
http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php