PHP+Mysql無重新整理問答評論系統詳解

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

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

針對某一篇文章進行評論

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("<p style='padding:20px 0;text-align:center;'><img src='" + site_url + "Public/images/loading.gif'></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 '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 ;

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.