【问题标题】:How to loop though a list of blog entries and all their comments on the same page如何在同一页面上循环浏览博客条目列表及其所有评论
【发布时间】:2010-11-27 10:04:36
【问题描述】:

我有一个这样的简单数据库结构:

entries table
+----+-----------+------------+-----------+------+
| id | date_time | post_title | post_slug | body |
+----+-----------+------------+-----------+------+

comments table
+----+-----------+----------+------+--------+
| id | date_time | entry_id | body | author |
+----+-----------+----------+------+--------+ 

我处于需要循环浏览同一页面上的所有 etnries 和所有 cmets 的情况。我该如何继续并以最好的方式做到这一点(性能和可维护性)

我是否需要先遍历所有条目,然后在该循​​环中获取所有的 cmets 并遍历它们?这意味着对数据库的大量查询。有没有更好的办法?

所需的输出如下所示:

Entry #1
    Comment #1
    Comment #2
Entry #2
    Comment #1
    Comment #2
    Comment #3
Entry #3
Entry #4
    Comment #1 

我希望这不是与其他帖子的重复,但我搜索了很长时间,没有找到任何东西......

谢谢!

【问题讨论】:

    标签: php activerecord codeigniter


    【解决方案1】:

    这对你有用吗?

    SELECT entries.id, entries.date_time, entries.post_title,
           entries.post_slug, entries.body, comments.id AS commentid,
           comments.date_time AS comment_date_time, comments.body AS commentbody
    FROM entries LEFT OUTER JOIN comments
    ON entries.id = comments.entry_id
    ORDER BY entries.id, comments.id
    

    现在迭代结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      相关资源
      最近更新 更多