欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP+Mysql无刷新问答评论系统(源码)

程序员文章站 2024-04-01 18:59:40
自己写的一个评论系统源码分享给大家,包括有表情,还有评论机制。用户名是随机的 针对某一篇文章进行评论 function subcomment() { $...

自己写的一个评论系统源码分享给大家,包括有表情,还有评论机制。用户名是随机的

针对某一篇文章进行评论

function subcomment() { 
  $data['uid'] = getuserid(); 
  $data['mtype'] = i("post.mtype", 0, 'int'); 
  if ($data['uid'] == '') { 
    echo json_encode(array("code" => -1)); 
  } else { 
    $content = addslashes(str_replace("\n", "<br />", $_post['content'])); 
    $data['tid'] = i("post.id", 0, 'int'); //文章id 
    if (strlen(preg_replace('/\[ [^\)]+? \]/x', '', $content)) < 10) { 
      echo json_encode(array("code" => "short than 10", "error" => "评论的内容不能少于10个字符。")); 
      exit; 
    } 
    if (c("db_pwd") != '') { 
      if (time() - session("comment_time") < 60 && session("comment_time") > 0) {//2分钟以后发布 
        echo json_encode(array("code" => "fast", "error" => "您提交评论的速度太快了,请稍后再发表评论。")); 
        exit; 
      } 
    } 
    $data['pid'] = i("post.pid", 0, 'int'); 
    $data['pid_sub'] = i("post.pid_sub", 0, 'int'); 
    $lyid = $data['pid_sub'] > 0 ? $data['pid_sub'] : $data['pid']; 
    if ($lyid > 0) { 
      $lyinfo = m("comment")->field("uid")->where("id='" . $lyid . "'")->find(); 
      $data['touid'] = $lyinfo['uid']; 
    } else { 
      $data['touid'] = 2; 
    } 
    $data['addtime'] = time(); 
    $emots = gettablefile("emot"); 
    foreach ($emots as $v) { 
      $content = str_replace("[" . $v['name'] . "]", "<img alt='" . $v['name'] . "' src='" . __app__ . "/public/emot/" . ($v['id'] - 1) . ".gif'>", $content); 
    } 
    $data['content'] = addslashes($content); 
    $info = m("comment")->field("id")->where("content='" . $data['content'] . "'")->find(); 
    if ($info['id']) { 
      echo json_encode(array("code" => "comment_repeat", "error" => "检测到重复评论,您似乎提交过这条评论了")); 
      exit; 
    } 
    $lastid = m("comment")->add($data); 
    $points_comment = 20; 
    if ($lastid > 0) { 
      $day_start = strtotime(date("y-m-d")); 
      $day_end = $day_start + 3600 * 24; 
      $comment_num_day = m("comment")->where("uid = " . $data['uid'] . " and addtime between " . $day_start . " and " . $day_end . "")->count(); 
      if ($comment_num_day <= 5) { //少于5条每天,则添加积分 
//          addpoints("comment", $points_comment, $data['uid'], "评论获得" . $points_comment . "积分", 5, 1); 
      } 
//        addmessage('comment', $data['tid'], $data['pid'], $data['mtype'], $data['touid'], $content); 
    } 
    session("comment_time", time()); 
    echo json_encode(array("code" => 200, "comment" => $content, "points" => $points_comment)); 
  } 
}

根据分页参数获取对应评论列表

function comments() { 
  $id = i("get.id", 0, 'int'); 
  $mtype = i("get.mtype", 1, 'int'); 
  $page = i("get.page", 1, "int"); 
  $totalnum = i("get.totalnum", 1, "int"); 
  $start = 10 * ($page - 1); 
  $sql = "tid = " . $id . " and pid = 0"; 
  $comments = m("comment")->field("id,uid,content,addtime")->where($sql)->order("id desc")->limit($start . ",10")->select(); 
//    echo m("comment")->getlastsql(); 
  foreach ($comments as $k => $v) { 
    $comments[$k]['sub'] = m("comment")->field("id,uid,content,pid_sub")->where("tid = " . $id . " and pid = " . $v['id'] . "")->order("id asc")->select(); 
  } 
  $this->assign("id", $id); 
  $this->assign("mtype", $mtype); 
  $this->assign("comments", $comments); 
  $this->assign("comments_num", $totalnum - ($page - 1) * 10); 
  $this->display(); 
}

切换评论分页

if ($("#detail-page").length > 0) { 
  var id = $("#detail-page").attr("data-id"); 
  var mtype = $("#detail-page").attr("data-mtype"); 
  var totalnum = $("#detail-page").attr("data-totalnum"); 
  $("#detail-page").children("a").click(function() { 
    var page = parseint($(this).attr("data-page")); 
    $("#detail-page").children("a").removeclass("current"); 
    $("#detail-page").children("a").eq(page - 1).addclass("current"); 
    $("#comment_list").html("<div style='padding:20px 0;text-align:center;'><img src='" + site_url + "public/images/loading.gif'></div>"); 
    $.get(geturl("box/comments"), { 
      page: page, 
      id: id, 
      totalnum: totalnum, 
      mtype: mtype 
    }, 
    function(data) { 
      $("#comment_list").html(data) 
    }) 
  }) 
}

评论表和表情表已放在压缩包里

create table if not exists `sucai_comment` ( 
 `id` int(11) not null auto_increment, 
 `uid` int(11) not null, 
 `touid` int(11) default '0', 
 `pid_sub` int(11) default '0', 
 `tid` int(11) not null, 
 `pid` int(11) default '0', 
 `mtype` tinyint(1) not null, 
 `content` text not null, 
 `addtime` int(10) not null, 
 primary key (`id`) 
) engine=myisam default charset=utf8 auto_increment=5560 ;

功能实现和demo原址: