问题描述
有人能为线程注释系统建议一个创新的数据库结构+提取算法,该算法每页输出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个查询构建主题注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!