2010-05-28 82 views
6

我想创建类似reddit的地方,他们有评论,然后回复评论,然后回复答复。如何回复评论? (PHP)

做什么类型的数据库结构,他们使用这样:

1. they keep track of all the comments to a posting 
2. a reply to a comment 
3. a reply to a reply 

所有我右边是仅仅是一个发布和一堆关于它像评论..

POSTING TABLE 
posting_id | title | author 

COMMENTS TABLE 
comment_id | posting_id | comment 

REPLIES TABLE 
???? 

怎么办我将评论与答复联系起来? 他们用什么类型的css来回复缩进空间?

编辑: 感谢您的答案!现在我唯一的问题是如何缩进回复? 如..

you like food 
    yes I love italian 
     Yes i do like it too 
    chinese is best 

回答

6

您可以另一列添加到您的意见表中指定,您与用户正在回复到注释(或回复)的ID来填充它parent_comment_id。在评论是对帖子的直接回复(而不是对评论的回复)的情况下,该栏将是空的。

+3

确切地说:评论是评论,无论他们是否对其他评论做出回复。 – 2010-05-28 23:20:34

+0

对不起,我太慢了。我知道了!谢谢。现在我的问题是如何缩进回复?我需要知道某种类型的关系吗?比如这个回复是第一个,这个回复是第二个缩进x2,这个回复是第三个,所以identx3 – jpjp 2010-05-28 23:27:58

+0

@jpjp:我会在应用程序级别执行此操作。当你构建评论树时,你知道很多东西要缩进它们。只是一个作为父母的一个。 – 2010-05-28 23:37:03

0

另一个字段添加到您的意见表,该表“REPLY_TO”或类似,并且储存它是在回答有注释的ID。

0

你可以把comments表一般像这样:

COMMENTS TABLE 
comment_id | posting_type | posting_id | comment 

其中posting_type是某种鉴别,例如串“POST”或“注释”,或为更高的效率(1 = POST整数,2 =评论等)。

编辑:无可否认,这是比较复杂的,但这意味着您可以对任何内容使用相同的评论表进行评论,而不仅仅是帖子和其他评论。

+0

您错过了与“父母”评论的关系。 – 2010-05-28 23:24:07

+0

不,因为然后posting_type是COMMENT,而posting_id是父注释ID。 – oedo 2010-05-28 23:24:54

+0

这打破了伪FK惯例。同样在这种情况下,您无法将表FK添加到表中,因为此时,posts_id也会引用自身。 – 2010-05-28 23:28:19

0

你不需要的答复表。正如其他人已经正确指出的那样,递归是RDBMS的一种方式。您始终可以考虑使用nosql样式的DBMS,以避免必须处理递归。

1

我会做一个交叉引用表。

实施例:

表:帖子

Columns: pstkey | userid | postMessage | etc...

pstkey是为后体的关键。用户标识是创建帖子的人。 postMessage是实际的帖子条目。

表:评论

Columns: comkey | pstkey | userid | commentMessage | etc...

comkey是提出的意见的关键。引用到使用pstkey的帖子。 userid是发表评论的人。然后commentMessage是实际评论的文本主体。

表:xref_postComm

Columns: xrefkey | pstkey | comkey | comkey2 |

现在最有趣的部分。所有帖子都进入帖子列表。所有评论进入评论表。这些关系全部在交叉参考表中定义。

我用这种方式完成了我的所有编程。我很荣幸与一位退休的世界级数据库工程师合作,他教会了我一些技巧。

如何使用交叉引用表:

xrefkey | pstkey | comkey | comkey2 
All that you look for is the population of a given field. 

xref (Auto Incremented) 
pstkey (Contains the pstkey for the post) 
comkey (Contains the comkey for the comment post) 
comkey2 (Contains the comkey for the comment post) 
     (but only populate comkey2 if comkey already has a value) 
and of course you populate comkey2 with the key of the comment. 

SEE, no reason for a 3rd tabel! 

With this method you can add as many relationships as you want. 
Now or in the future! 

comkey2是回复您的回复。该单行所包含的位置......帖子的关键字,评论的关键字以及回复评论的回复关键字。全部由外部参照的人口完成。

 
EXAMPLE: 
PAGES.... Page table 

POSTS 
pstkey | pageid | user| Post 
------------------------------------- 
| 1 | 1 | 45 | Went to the store the....| 
| 2 | 2 | 18 | Saw an apple on tv..... 

COMMENTS 
comkey | pstkey | user | Comment 
----------------------------------------------- 
| 1 | 1 | 9 | Wanted to say thanks... 
| 2 | 1 | 7 | Cool I like tha..... 
| 3 | 2 | 3 | Great seeing ya.... 
| 4 | 2 | 6 | Had a great.... 
| 5 | 2 | 2 | Don't sweat it man... 

xref_PostCom 
xrefkey | pageid | pstkey | comkey | comkey2 | 
---------------------------------------------- 
| 1 | 1 | 1 | NULL | NULL | Post1 on Page1 
| 2 | 1 | 1 | 1 | NULL | Comment1 under Post1 
| 3 | 1 | 1 | 2 | NULL | Comment2 under Post1 
| 4 | 2 | 2 | NULL | NULL | Post2 on Page2 
| 5 | 2 | 2 | 3 | NULL | Comment3 under Post2 on Page2 
| 6 | 2 | 2 | 4 | NULL | Comment4 under Post2 on Page2 (a second Comment) 
| 7 | 2 | 2 | 4 | 5 | Explained below.... 
Comment key 5 is matched with comment key 4....under post2 on Page 2 

如果你知道什么加盟,左连接,右连接,内/外连接创建选择的利用这些关系来获得数据数组,你的工作变得轻松许多。

我相信这位工程师称这基本上是定义关系的“数据图”。现在的诀窍是你如何使用这些关系访问它们。它一开始很难接近,但知道我所知道的,我拒绝以其他方式去做。

最后会发生什么?你最终会写出一个脚本说,好吧,去做呃,一切,然后回来。您将以1个函数调用请求第1页。它将返回第1页,第1页,第1条评论,第&和& 3,并回复1个数组中的答复。回声输出并完成。

更新评论 我在第一次向我展示时说了同样的事情。事实上,数据库程序员迫使我这样做,真的让我发疯。但现在我明白了。好处很多。

优点1)可以写入1个查询,将其全部抽出。

2)在多个查询的答案可以在一个结构中填充数组,在打印页面时,循环中的循环可以显示页面。

3)升级使用它的软件可以支持任何您可能想到的可能的设计更改。无懈可击的可扩展性。

教给我的那个人是重新设计了sears和jcpenny数据库的雇佣枪手。回到他们有9本书因为重复记录问题而去同一栋房子的时候。

交叉引用表避免了设计中的很多问题。

这个理论的核心是,一个列不仅可以包含数据,而且可以同时用作真或假陈述。这是自我节省空间。为什么要搜索20个表格? 1索引交叉引用表可以告诉你关于其他20个表的所有信息,它的内容,你需要什么,什么你不需要,甚至你甚至需要打开另一个表。

简称: 1含什么,但INT(2/11)交叉引用,告诉你,你需要知道你曾经打开另一个表之前的一切东西,不仅包含了完美无瑕的可扩展性,但照明效果的速度。更不用说重复记录的可能性很小。对你和我来说重复记录可能不是问题。但是,对于西尔斯有40亿美元的记录,每本11美元,就会增加错误。

+0

你能说这有什么好处吗?我所看到的是我必须做更多的连接...... – 2010-05-29 00:41:38

2

要在回复中显示回复,您必须进行递归调用才能继续生成子回复。

喜欢的东西

function get_comments($comment_id) { 
    print '<div class="comment_body">'; 

    // print comment body or something? 

    if (comment_has_reply($comment_id)) { 
     foreach(comment_comments($comment_id) as $comment) { 
      get_comments($comment->id); 
     } 
    } 

    print '</div>'; 
} 

要然而缩进注释,使用CSS。

<style type="text/css"> 
.comment_body { 
    margin-left:10px; 
} 
</style> 

这样子回复缩进比父母更多,他们的潜艇被缩进甚至更多,等等。