php实现文章评论系统

php实现文章评论系统

最近工作中需要完成一个评论的功能,上网查找了几个评论系统的展示样式。最后参考“多说”和“畅言”等评论系统,自己使用PHP语言实现了一个简单的评论系统。并记录了两种方式(递归方式和非递归方式)的实现过程,以及分析两种方式的优缺点,但前端如何实现就没有展现了。

首先设计数据库如下:

create table comments(
id bigint unsigned not null AUTO_INCREMENT,
arc_id bigint unsigned not null COMMENT ‘文章id’,
user_id bigint unsigned not null COMMENT ‘用户id’,
comment_id bigint unsigned not null DEFAULT ‘0’ COMMENT ‘回复某个评论的id’,
content varchar(255) not null DEFAULT ‘’ COMMENT ‘评论或回复的内容’,
add_time timestamp not null DEFAULT CURRENT_TIMESTAMP COMMENT ‘添加时间’,
PRIMARY KEY (id),
KEY arc_id (arc_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT ‘文章评论表’;
创建测试数据如下:

具体实现方案如下(在ThinkPHP框架上实现):

1、递归方式

优点:实现代码简单,而且如果评论的层级固定在5个层次一下的话,建议使用该种方法,这样前端通过这种数据结果实现简单。

缺点:如果评论的层级没有固定的话,前端将无法展示评论信息了,而且如果层级太多的话,将会极大的消耗内存,更要命的是每次递归都得查询数据库,性能将大大的降低。

/**

  • @param $arc_id 文章id
  • @param int $comm_id 评论id
  • @param array $result
  • @return array
    */
    function getCommlist($arc_id, KaTeX parse error: Expected 'EOF', got '&' at position 14: comm_id = 0, &̲result = array()){ //获取评论列表
    if(empty($arc_id)){
    return array();
    }
    KaTeX parse error: Expected '}', got 'EOF' at end of input: …e = "arc_id = { arc_id} AND comment_id = {$comm_id}";
    r e s = M ( ′ c o m m e n t s ′ ) − > w h e r e ( res = M('comments')->where( res=M(comments)>where(_where)->order(‘add_time DESC’)->select();
    if(empty( r e s ) ) r e t u r n a r r a y ( ) ; f o r e a c h ( res)){ return array(); } foreach ( res))returnarray();for
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值