首頁 > 後端開發 > php教程 > PHP+Mysql無刷新問答評論系統詳解

PHP+Mysql無刷新問答評論系統詳解

墨辰丷
發布: 2023-03-28 10:46:01
原創
1916 人瀏覽過

自己寫的一個評論系統源碼分享給大家,包括有表情,還有評論機制,代碼簡單易懂,需要的朋友參考下

自己寫的一個評論系統源碼分享給大家,包括有表情,還有評論機制。使用者名稱是隨機的

針對某一篇文章進行評論

#
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[&#39;content&#39;])); 
    $data[&#39;tid&#39;] = I("post.id", 0, &#39;int&#39;); //文章id 
    if (strlen(preg_replace(&#39;/\[ [^\)]+? \]/x&#39;, &#39;&#39;, $content)) < 10) { 
      echo json_encode(array("code" => "short than 10", "error" => "评论的内容不能少于10个字符。")); 
      exit; 
    } 
    if (C("DB_PWD") != &#39;&#39;) { 
      if (time() - session("comment_time") < 60 && session("comment_time") > 0) {//2分钟以后发布 
        echo json_encode(array("code" => "fast", "error" => "您提交评论的速度太快了,请稍后再发表评论。")); 
        exit; 
      } 
    } 
    $data[&#39;pid&#39;] = I("post.pid", 0, &#39;int&#39;); 
    $data[&#39;pid_sub&#39;] = I("post.pid_sub", 0, &#39;int&#39;); 
    $lyid = $data[&#39;pid_sub&#39;] > 0 ? $data[&#39;pid_sub&#39;] : $data[&#39;pid&#39;]; 
    if ($lyid > 0) { 
      $lyinfo = M("comment")->field("uid")->where("id=&#39;" . $lyid . "&#39;")->find(); 
      $data[&#39;touid&#39;] = $lyinfo[&#39;uid&#39;]; 
    } else { 
      $data[&#39;touid&#39;] = 2; 
    } 
    $data[&#39;addtime&#39;] = time(); 
    $emots = getTableFile("emot"); 
    foreach ($emots as $v) { 
      $content = str_replace("[" . $v[&#39;name&#39;] . "]", "<img alt=&#39;" . $v[&#39;name&#39;] . "&#39; src=&#39;" . __APP__ . "/Public/emot/" . ($v[&#39;id&#39;] - 1) . ".gif&#39;>", $content); 
    } 
    $data[&#39;content&#39;] = addslashes($content); 
    $info = M("comment")->field("id")->where("content=&#39;" . $data[&#39;content&#39;] . "&#39;")->find(); 
    if ($info[&#39;id&#39;]) { 
      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[&#39;uid&#39;] . " AND addtime between " . $day_start . " AND " . $day_end . "")->count(); 
      if ($comment_num_day <= 5) { //少于5条每天,则添加积分 
//          addPoints("comment", $points_comment, $data[&#39;uid&#39;], "评论获得" . $points_comment . "积分", 5, 1); 
      } 
//        addMessage(&#39;comment&#39;, $data[&#39;tid&#39;], $data[&#39;pid&#39;], $data[&#39;mtype&#39;], $data[&#39;touid&#39;], $content); 
    } 
    session("comment_time", time()); 
    echo json_encode(array("code" => 200, "comment" => $content, "points" => $points_comment)); 
  } 
}
登入後複製

根據分頁參數獲取對應評論清單

function comments() { 
  $id = I("get.id", 0, &#39;int&#39;); 
  $mtype = I("get.mtype", 1, &#39;int&#39;); 
  $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][&#39;sub&#39;] = M("comment")->field("id,uid,content,pid_sub")->where("tid = " . $id . " AND pid = " . $v[&#39;id&#39;] . "")->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("<p style=&#39;padding:20px 0;text-align:center;&#39;><img src=&#39;" + site_url + "Public/images/loading.gif&#39;></p>"); 
    $.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 &#39;0&#39;, 
 `pid_sub` int(11) DEFAULT &#39;0&#39;, 
 `tid` int(11) NOT NULL, 
 `pid` int(11) DEFAULT &#39;0&#39;, 
 `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 ;
登入後複製

以上就是本文的全部內容,希望對大家的學習有所幫助。


相關推薦:

php實作評論回覆刪除功能實例詳解

#php無限級評論嵌套實現步驟詳解

#php無限級評論嵌套實作步驟詳解

以上是PHP+Mysql無刷新問答評論系統詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板