如何使用1或2个查询构建主题注释? [英] How to build Threaded comments with a 1 or 2 queries?

查看:78
本文介绍了如何使用1或2个查询构建主题注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能为线程注释系统建议一个创新的数据库结构+提取算法,该算法每页输出x个线程(每个线程无限制答复)吗?

Can anyone suggest a creative database structure + fetching algorithm for a threaded comments system, that would output x amount of threads per page (with unlimited replies for each)?

我可以运行查询以获取线程,并在循环的每个实例中运行另一个查询以回显答复....但这是一个坏主意.

I can run a query to get the threads, and in each instance of a loop, run another query to echo out the replies.... but that's a bad idea.

推荐答案

如果只需要2个级别,则可以通过一种查询方式进行操作:

If you need only 2 levels, here's a way with one query:

您的表-id, parent_id, comment

代码

$rows = mysql_query('
  select *
  FROM
    comments
  ORDER BY
    id DESC');

$threads = array();
foreach($rows as $row) {
  if($row['parent_id'] === '0') {
    $threads[$row['id']] = array(
      'comment' => $row['comment'],
      'replies' => array()
    );
  } else {
    $threads[$row['parent_id']]['replies'][] = $row['comment'];
  }
}

$threads中,您将拥有所有主线程,而$threads[$id]['replies']将保留所有答复.线程已排序-最新=首先,添加一些页面调度,您就可以开始了.

In $threads you will have all your main threads and $threads[$id]['replies'] holds all replies. The threads are sorted - latest = first, add some paging and you're good to go.

这篇关于如何使用1或2个查询构建主题注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆