国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

thinkPHP實現基于ajax的評論回復功能

2024-05-05 00:04:19
字體:
來源:轉載
供稿:網友

本文實例講述了thinkPHP實現基于ajax的評論回復功能。分享給大家供大家參考,具體如下:

控制器代碼:

<?phpnamespace Home/Controller;use Think/Controller;class IndexController extends Controller {  public function index(){    $num = M('comment')->count(); //獲取評論總數    $this->assign('num',$num);    $data=array();    $data=$this->getCommlist();//獲取評論列表    $this->assign("commlist",$data);    $this->display('index');  }  /**  *添加評論  */  public function addComment(){    $data=array();    if((isset($_POST["comment"]))&&(!empty($_POST["comment"]))){      $cm = json_decode($_POST["comment"],true);//通過第二個參數true,將json字符串轉化為鍵值對數組      $cm['create_time']=date('Y-m-d H:i:s',time());      $newcm = M('comment');      $id = $newcm->add($cm);      $cm["id"] = $id;      $data = $cm;      $num = M('comment')->count();//統計評論總數      $data['num']= $num;    }else{      $data["error"] = "0";    }    echo json_encode($data);  }  /**  *遞歸獲取評論列表  */  protected function getCommlist($parent_id = 0,&$result = array()){    $arr = M('comment')->where("parent_id = '".$parent_id."'")->order("create_time desc")->select();    if(empty($arr)){      return array();    }    foreach ($arr as $cm) {      $thisArr=&$result[];      $cm["children"] = $this->getCommlist($cm["id"],$thisArr);      $thisArr = $cm;    }    return $result;  }}

JavaScript代碼:

$(function(){  //點擊提交評論內容  $('body').delegate('.comment-submit','click',function(){    var content = $.trim($(this).parent().prev().children("textarea").val());//根據布局結構獲取當前評論內容    $(this).parent().prev().children("textarea").val("");//獲取完內容后清空輸入框    if(""==content){      alert("評論內容不能為空!");    }else{      var cmdata = new Object();      cmdata.parent_id = $(this).attr("parent_id");//上級評論id      cmdata.content = content;      cmdata.nickname = "游客";//測試用數據      cmdata.head_pic = "/Public/images/default.jpg";//測試用數據      var replyswitch = $(this).attr("replyswitch");//獲取回復開關鎖屬性      $.ajax({        type:"POST",        url:"/index.php/home/index/addComment",        data:{          comment:JSON.stringify(cmdata)        },        dataType:"json",        success:function(data){          if(typeof(data.error)=="undefined"){            $(".comment-reply").next().remove();//刪除已存在的所有回復div            //更新評論總數            $(".comment-num").children("span").html(data.num+"條評論");            //顯示新增評論            var newli = "";            if(cmdata.parent_id == "0"){             //發表的是一級評論時,添加到一級ul列表中             newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"'              $(".comment-ul").prepend(newli);            }else{             //否則添加到對應的孩子ul列表中              if('off'==replyswitch){//檢驗出回復關閉鎖存在,即三級評論不再提供回復功能                newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"'              }else{//二級評論的回復按鈕要添加回復關閉鎖屬性                newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"'              }              $("li[comment_id='"+data.parent_id+"']").children("ul").prepend(newli);            }          }else{            //有錯誤信息            alert(data.error);          }        }      });    }  });  //點擊"回復"按鈕顯示或隱藏回復輸入框  $("body").delegate(".comment-reply","click",function(){    if($(this).next().length>0){//判斷出回復div已經存在,去除掉      $(this).next().remove();     }else{//添加回復div      $(".comment-reply").next().remove();//刪除已存在的所有回復div      //添加當前回復div      var parent_id = $(this).attr("comment_id");//要回復的評論id      var divhtml = "";      if('off'==$(this).attr("replyswitch")){//二級評論回復后三級評論不再提供回復功能,將關閉屬性附加到"提交回復"按鈕"        divhtml = "<div class='div-reply-txt' style='width:98%;padding:3px;' replyid='2'><div><textarea class='txt-reply' replyid='2' style='width: 100%; height: 60px;'></textarea></div><div style='margin-top:5px;text-align:right;'><a class='comment-submit' parent_id='"+parent_id+"' style='font-size:14px;text-decoration:none;background-color:#63B8FF;' href='javascript:void(0);' replyswitch='off' >提交回復</a></div></div>";      }else{        divhtml = "<div class='div-reply-txt' style='width:98%;padding:3px;' replyid='2'><div><textarea class='txt-reply' replyid='2' style='width: 100%; height: 60px;'></textarea></div><div style='margin-top:5px;text-align:right;'><a class='comment-submit' parent_id='"+parent_id+"' style='font-size:14px;text-decoration:none;background-color:#63B8FF;' href='javascript:void(0);'>提交回復</a></div></div>";      }      $(this).after(divhtml);     }  });})

頁面樣式代碼:

.comment-filed{  width:640px;  margin:0 auto;}.comment-num{  text-align: right;  font-size:14px;}.div-txt-submit{  text-align:right;  margin-top:8px;}.comment-submit{  background-color:#63B8FF;  margin-top:15px;  text-decoration:none;  color:#fff;  padding:5px;  font-size:14px;}.txt-commit{  border:1px solid blue;  width:620px;  height: 60px;  padding: 10px;}.txt-reply{  width: 100%;  height: 60px;}.comment-filed-list{  margin-top:20px;}.comment-list{  margin-top:2px;  width:herit;  height:50px;  border-top:1px solid gray;}.comment-ul{  list-style:none;  padding-left:0;}.head-pic{  width:40px;  height:40px;}.cm{  position:relative;  top:0px;  left:40px;  top:-40px;  width:600px;}.cm-header{  padding-left:5px;}.cm-content{  padding-left:5px;}.cm-footer{  padding-bottom:15px;  text-align:right;  border-bottom: 1px dotted #CCC;}.comment-reply{  text-decoration:none;  color:gray;  font-size: 14px;}.children{  list-style:none;  background-color:#FAFAFA;  padding-left:0;  margin-left:40px;}.children-cm{  position:relative;  left:40px;  top:-40px;  width:90%;}

頁面布局代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html lang="en"><head>  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">  <title>php評論及回復功能</title>  <link rel="stylesheet" type="text/css" href="/Public/css/comment.css" rel="external nofollow" >  <script type="text/javascript" src="/Public/js/jquery-1.11.3.min.js" ></script>  <script type="text/javascript" src="/Public/js/comment.js" ></script></head><body><div class="comment-filed"> <!--發表評論區begin--> <div>  <div class="comment-num">    <span>{$num}條評論</span>  </div>  <div>    <div>    <textarea class="txt-commit" replyid="0"></textarea>    </div>    <div class="div-txt-submit">      <a class="comment-submit" parent_id="0" style="" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span style=''>發表評論</span></a>    </div>  </div> </div> <!--發表評論區end--> <!--評論列表顯示區begin-->  <!-- {$commentlist} -->  <div class="comment-filed-list" >    <div><span>全部評論</span></div>    <div class="comment-list" >      <!--一級評論列表begin-->      <ul class="comment-ul">        <volist name="commlist" id="data">          <li comment_id="{$data.id}">          <div >            <div>              <img class="head-pic" src="{$data.head_pic}"            </div>            <div class="cm">              <div class="cm-header">              <span>{$data.nickname}</span>              <span>{$data.create_time}</span>              </div>              <div class="cm-content">                <p>                  {$data.content}                </p>              </div>              <div class="cm-footer">                <a class="comment-reply" comment_id="{$data.id}" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >回復</a>              </div>            </div>          </div>          <!--二級評論begin-->          <ul class="children">            <volist name="data.children" id="child" >            <li comment_id="{$child.id}">              <div >                <div>                  <img class="head-pic" src="{$child.head_pic}"                </div>                <div class="children-cm">                  <div class="cm-header">                  <span>{$child.nickname}</span>                  <span>{$child.create_time}</span>                  </div>                  <div class="cm-content">                    <p>                      {$child.content}                    </p>                  </div>                  <div class="cm-footer">                    <a class="comment-reply" replyswitch="off" comment_id="{$child.id}" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >回復</a>                  </div>                </div>              </div>              <!--三級評論begin-->              <ul class="children">                <volist name="child.children" id="grandson" >                <li comment_id="{$grandson.id}">                  <div >                    <div>                      <img class="head-pic" src="{$grandson.head_pic}"                    </div>                    <div class="children-cm">                      <div class="cm-header">                      <span>{$grandson.nickname}</span>                      <span>{$grandson.create_time}</span>                      </div>                      <div class="cm-content">                        <p>                          {$grandson.content}                        </p>                      </div>                      <div class="cm-footer">                        <!-- <a class="comment-reply" comment_id="1" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >回復</a> -->                      </div>                    </div>                  </div>                </li>                </volist>              </ul>              <!--三級評論end-->            </li>            </volist>          </ul>          <!--二級評論end-->        </li>        </volist>      </ul>      <!--一級評論列表end-->    </div>  </div> <!--評論列表顯示區end--></div></body></html>

sql語句:

DROP TABLE IF EXISTS `t_comment`;CREATE TABLE `t_comment` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵id', `parent_id` int(11) NOT NULL COMMENT '上級評論id,若是一級評論則為0', `nickname` varchar(100) DEFAULT NULL COMMENT '評論人昵稱', `head_pic` varchar(400) DEFAULT NULL COMMENT '評論人頭像', `content` text COMMENT '評論內容', `create_time` datetime DEFAULT NULL COMMENT '評論或回復發表時間', PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=148 DEFAULT CHARSET=utf8;

頁面布局少一個jquery.js請自行加上。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 修武县| 临高县| 宁安市| 娄底市| 政和县| 香格里拉县| 公主岭市| 酒泉市| 乐平市| 华亭县| 房山区| 泽州县| 榆树市| 朝阳市| 张家口市| 咸宁市| 周宁县| 喀喇沁旗| 阿拉善盟| 彭水| 蒙山县| 治县。| 高雄市| 阿坝县| 四平市| 巢湖市| 韶关市| 喀喇沁旗| 扎兰屯市| 武宁县| 浮梁县| 井陉县| 子洲县| 万山特区| 中江县| 田林县| 高要市| 沈阳市| 高邮市| 汝城县| 兴仁县|