最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

thinkPHP實(shí)現(xiàn)基于ajax的評(píng)論回復(fù)功能

 更新時(shí)間:2018年06月22日 08:38:04   作者:Eternalxc  
這篇文章主要介紹了thinkPHP實(shí)現(xiàn)基于ajax的評(píng)論回復(fù)功能,結(jié)合實(shí)例形式分析了thinkPHP實(shí)現(xiàn)ajax評(píng)論回復(fù)所涉及的控制器、視圖、樣式、js使用post進(jìn)行ajax提交、并附帶了相應(yīng)的SQL語(yǔ)句,需要的朋友可以參考下

本文實(shí)例講述了thinkPHP實(shí)現(xiàn)基于ajax的評(píng)論回復(fù)功能。分享給大家供大家參考,具體如下:

控制器代碼:

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
  public function index(){
    $num = M('comment')->count(); //獲取評(píng)論總數(shù)
    $this->assign('num',$num);
    $data=array();
    $data=$this->getCommlist();//獲取評(píng)論列表
    $this->assign("commlist",$data);
    $this->display('index');
  }
  /**
  *添加評(píng)論
  */
  public function addComment(){
    $data=array();
    if((isset($_POST["comment"]))&&(!empty($_POST["comment"]))){
      $cm = json_decode($_POST["comment"],true);//通過(guò)第二個(gè)參數(shù)true,將json字符串轉(zhuǎn)化為鍵值對(duì)數(shù)組
      $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();//統(tǒng)計(jì)評(píng)論總數(shù)
      $data['num']= $num;
    }else{
      $data["error"] = "0";
    }
    echo json_encode($data);
  }
  /**
  *遞歸獲取評(píng)論列表
  */
  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(){
  //點(diǎn)擊提交評(píng)論內(nèi)容
  $('body').delegate('.comment-submit','click',function(){
    var content = $.trim($(this).parent().prev().children("textarea").val());//根據(jù)布局結(jié)構(gòu)獲取當(dāng)前評(píng)論內(nèi)容
    $(this).parent().prev().children("textarea").val("");//獲取完內(nèi)容后清空輸入框
    if(""==content){
      alert("評(píng)論內(nèi)容不能為空!");
    }else{
      var cmdata = new Object();
      cmdata.parent_id = $(this).attr("parent_id");//上級(jí)評(píng)論id
      cmdata.content = content;
      cmdata.nickname = "游客";//測(cè)試用數(shù)據(jù)
      cmdata.head_pic = "/Public/images/default.jpg";//測(cè)試用數(shù)據(jù)
      var replyswitch = $(this).attr("replyswitch");//獲取回復(fù)開(kāi)關(guān)鎖屬性
      $.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();//刪除已存在的所有回復(fù)div
            //更新評(píng)論總數(shù)
            $(".comment-num").children("span").html(data.num+"條評(píng)論");
            //顯示新增評(píng)論
            var newli = "";
            if(cmdata.parent_id == "0"){
             //發(fā)表的是一級(jí)評(píng)論時(shí),添加到一級(jí)ul列表中
             newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"' alt=''></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);'>回復(fù)</a></div></div></div><ul class='children'></ul></li>";
              $(".comment-ul").prepend(newli);
            }else{
             //否則添加到對(duì)應(yīng)的孩子ul列表中
              if('off'==replyswitch){//檢驗(yàn)出回復(fù)關(guān)閉鎖存在,即三級(jí)評(píng)論不再提供回復(fù)功能
                newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"' alt=''></div><div class='children-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'></div></div></div><ul class='children'></ul></li>";
              }else{//二級(jí)評(píng)論的回復(fù)按鈕要添加回復(fù)關(guān)閉鎖屬性
                newli = "<li comment_id='"+data.id+"'><div ><div><img class='head-pic' src='"+data.head_pic+"' alt=''></div><div class='children-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);' replyswitch='off' >回復(fù)</a></div></div></div><ul class='children'></ul></li>";
              }
              $("li[comment_id='"+data.parent_id+"']").children("ul").prepend(newli);
            }
          }else{
            //有錯(cuò)誤信息
            alert(data.error);
          }
        }
      });
    }
  });
  //點(diǎn)擊"回復(fù)"按鈕顯示或隱藏回復(fù)輸入框
  $("body").delegate(".comment-reply","click",function(){
    if($(this).next().length>0){//判斷出回復(fù)div已經(jīng)存在,去除掉
      $(this).next().remove();
     }else{//添加回復(fù)div
      $(".comment-reply").next().remove();//刪除已存在的所有回復(fù)div
      //添加當(dāng)前回復(fù)div
      var parent_id = $(this).attr("comment_id");//要回復(fù)的評(píng)論id
      var divhtml = "";
      if('off'==$(this).attr("replyswitch")){//二級(jí)評(píng)論回復(fù)后三級(jí)評(píng)論不再提供回復(fù)功能,將關(guān)閉屬性附加到"提交回復(fù)"按鈕"
        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' >提交回復(fù)</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);'>提交回復(fù)</a></div></div>";
      }
      $(this).after(divhtml);
     }
  });
})

頁(yè)面樣式代碼:

.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%;
}

頁(yè)面布局代碼:

<!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評(píng)論及回復(fù)功能</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">
 <!--發(fā)表評(píng)論區(qū)begin-->
 <div>
  <div class="comment-num">
    <span>{$num}條評(píng)論</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=''>發(fā)表評(píng)論</span></a>
    </div>
  </div>
 </div>
 <!--發(fā)表評(píng)論區(qū)end-->
 <!--評(píng)論列表顯示區(qū)begin-->
  <!-- {$commentlist} -->
  <div class="comment-filed-list" >
    <div><span>全部評(píng)論</span></div>
    <div class="comment-list" >
      <!--一級(jí)評(píng)論列表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}" alt="">
            </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" >回復(fù)</a>
              </div>
            </div>
          </div>
          <!--二級(jí)評(píng)論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}" alt="">
                </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" >回復(fù)</a>
                  </div>
                </div>
              </div>
              <!--三級(jí)評(píng)論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}" alt="">
                    </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" >回復(fù)</a> -->
                      </div>
                    </div>
                  </div>
                </li>
                </volist>
              </ul>
              <!--三級(jí)評(píng)論end-->
            </li>
            </volist>
          </ul>
          <!--二級(jí)評(píng)論end-->
        </li>
        </volist>
      </ul>
      <!--一級(jí)評(píng)論列表end-->
    </div>
  </div>
 <!--評(píng)論列表顯示區(qū)end-->
</div>
</body>
</html>

sql語(yǔ)句:

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 '上級(jí)評(píng)論id,若是一級(jí)評(píng)論則為0',
 `nickname` varchar(100) DEFAULT NULL COMMENT '評(píng)論人昵稱',
 `head_pic` varchar(400) DEFAULT NULL COMMENT '評(píng)論人頭像',
 `content` text COMMENT '評(píng)論內(nèi)容',
 `create_time` datetime DEFAULT NULL COMMENT '評(píng)論或回復(fù)發(fā)表時(shí)間',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=148 DEFAULT CHARSET=utf8;

頁(yè)面布局少一個(gè)jquery.js請(qǐng)自行加上。

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Laravel自定義 封裝便捷返回Json數(shù)據(jù)格式的引用方法

    Laravel自定義 封裝便捷返回Json數(shù)據(jù)格式的引用方法

    今天小編就為大家分享一篇Laravel自定義 封裝便捷返回Json數(shù)據(jù)格式的引用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-09-09
  • CI框架常用經(jīng)典操作類總結(jié)(路由,偽靜態(tài),分頁(yè),session,驗(yàn)證碼等)

    CI框架常用經(jīng)典操作類總結(jié)(路由,偽靜態(tài),分頁(yè),session,驗(yàn)證碼等)

    這篇文章主要介紹了CI框架常用經(jīng)典操作類,結(jié)合實(shí)例形式總結(jié)分析了CI框架URL、路由、偽靜態(tài)、分頁(yè)、session、驗(yàn)證碼等相關(guān)操作類與使用技巧,需要的朋友可以參考下
    2016-11-11
  • 詳解php如何對(duì)數(shù)組進(jìn)行排序

    詳解php如何對(duì)數(shù)組進(jìn)行排序

    這篇文章主要為大家詳細(xì)介紹了php如何對(duì)數(shù)組進(jìn)行排序,合并,截取替換等操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2023-09-09
  • 使用PHP實(shí)現(xiàn)一個(gè)輕量級(jí)HTML模板引擎

    使用PHP實(shí)現(xiàn)一個(gè)輕量級(jí)HTML模板引擎

    在Web開(kāi)發(fā)中,我們經(jīng)常需要?jiǎng)討B(tài)生成HTML頁(yè)面,為了提高開(kāi)發(fā)效率和代碼可維護(hù)性,使用模板引擎是一個(gè)非常普遍的方案,本文將介紹如何使用PHP實(shí)現(xiàn)一個(gè)簡(jiǎn)單的HTML模板引擎,滿足變量綁定輸出和if判斷的需求,需要的朋友可以參考下
    2023-08-08
  • 關(guān)于laravel后臺(tái)模板laravel-admin select框的使用詳解

    關(guān)于laravel后臺(tái)模板laravel-admin select框的使用詳解

    今天小編就為大家分享一篇關(guān)于laravel后臺(tái)模板laravel-admin select框的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • PHP、Python和Javascript的裝飾器模式對(duì)比

    PHP、Python和Javascript的裝飾器模式對(duì)比

    這篇文章主要介紹了PHP、Python和Javascript的裝飾器模式對(duì)比,修飾模式(Decorator Pattern),又叫裝飾者模式,是面向?qū)ο缶幊填I(lǐng)域中,一種動(dòng)態(tài)地往一個(gè)類中添加新的行為的設(shè)計(jì)模式,需要的朋友可以參考下
    2015-02-02
  • php fseek函數(shù)讀取大文件兩種方法

    php fseek函數(shù)讀取大文件兩種方法

    php中讀取文件非常簡(jiǎn)單,但如果讀取的文件非常大,改如何解決呢?我們可以直接使用fseek來(lái)進(jìn)行大文件操作,本文章向大家介紹php使用fseek函數(shù)讀取大文件,需要的朋友可以參考一下
    2016-10-10
  • 詳解PHP發(fā)送郵件知識(shí)點(diǎn)

    詳解PHP發(fā)送郵件知識(shí)點(diǎn)

    本片文章我們給大家總結(jié)了PHP中發(fā)送郵件的相關(guān)知識(shí)點(diǎn)以及詳細(xì)用法代碼,有需要的朋友學(xué)習(xí)下吧。
    2018-05-05
  • PHP使用遞歸按層級(jí)查找數(shù)據(jù)的方法

    PHP使用遞歸按層級(jí)查找數(shù)據(jù)的方法

    這篇文章主要介紹了PHP使用遞歸按層級(jí)查找數(shù)據(jù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • ThinkPHP中l(wèi)imit()使用方法詳解

    ThinkPHP中l(wèi)imit()使用方法詳解

    本文介紹ThinkPHP的limit()方法的用法。limit方法可以用于對(duì)數(shù)據(jù)庫(kù)操作的結(jié)果進(jìn)行取指定范圍的條數(shù)。即相當(dāng)于是在mysql查詢語(yǔ)句中的limit子句
    2016-04-04

最新評(píng)論

阿城市| 潮州市| 铁岭县| 伊金霍洛旗| 和顺县| 东兰县| 焦作市| 封丘县| 乐安县| 宜兰县| 达孜县| 广宗县| 桃园县| 革吉县| 上高县| 大同市| 台南市| 松溪县| 精河县| 武胜县| 武穴市| 海晏县| 两当县| 西安市| 江阴市| 黎平县| 广东省| 宁晋县| 夏邑县| 措美县| 宁津县| 屏山县| 张家界市| 平遥县| 蚌埠市| 炉霍县| 梧州市| 兴海县| 容城县| 吉林省| 仪征市|