基於Thinkphp開發的一個簡單的帶表情的評論回覆實例,能夠無限回覆,適合新手學習或做爲畢業設計做品等。javascript
評論提交驗證php
$(".submit-btn").click(function() { var $this = $(this); var name = $this.parent().siblings().children('.name1').val(); var content = $this.parent().siblings().children('.comment').val(); if (name == "" || content == "") { alert("暱稱或者評論不能爲空哦"); return false; } });
添加評論java
$rules = array(//定義動態驗證規則 array('comment', 'require', '評論不能爲空'), array('username', 'require', '暱稱不能爲空'), // array('username', '3,15', '用戶名長度必須在3-15位之間!', 0, 'length', 3), ); $data = array( 'content' => I("post.comment"), 'ip' => get_client_ip(), 'add_time' => time(), 'pid' => I('post.pid'), 'author' => I('post.username'), ); $comment = M("comment"); // 實例化User對象 if (!$comment->validate($rules)->create()) {//驗證暱稱和評論 exit($comment->getError()); } else { $add = $comment->add($data); if ($add) { $this->success('評論成功'); } else { $this->error('評論失敗'); } }
評論遞歸函數函數
function CommentList($pid = 0, &$commentList = array(), $spac = 0) { static $i = 0; $spac = $spac + 1; //初始爲1級評論 $List = M('comment')-> field('id,add_time,author,content,pid')-> where(array('pid' => $pid))->order("id DESC")->select(); foreach ($List as $k => $v) { $commentList[$i]['level'] = $spac; //評論層級 $commentList[$i]['author'] = $v['author']; $commentList[$i]['id'] = $v['id']; $commentList[$i]['pid'] = $v['pid']; //此條評論的父id $commentList[$i]['content'] = $v['content']; $commentList[$i]['time'] = $v['add_time']; // $commentList[$i]['pauthor']=$pautor; $i++; $this->CommentList($v['id'], $commentList, $spac); } return $commentList; }