您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關(guān)利用php怎么一個評論回復(fù)和動態(tài)刪除功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
一、思路如下:
(1)你發(fā)表動態(tài)之后,會有人評論這一條動態(tài),當(dāng)評論之后,你也會回復(fù)該評論;(此處評論要單獨一張表,回復(fù)也要單獨一張表)
(2)刪除動態(tài):會將動態(tài)連同評論、回復(fù)全部刪除;刪除評論:只會刪除該條評論
二、在寫代碼之前,我還是想把流程說一遍:
(1)發(fā)表動態(tài)---評論---回復(fù)---再回復(fù)
(2)將上邊的流程細化,我先在紙上寫出,再上傳,碼字不能表達清楚(注意的是,我想要的功能的實現(xiàn),并不是一模一樣的哈)
三、還是先將代碼分塊解釋,最后將主頁面代碼完全附上(含上一篇)
在上一篇中已經(jīng)實現(xiàn)發(fā)布動態(tài)、彈出評論框,那么現(xiàn)在接著向下走:
分別看一下qqfriends,qqdongtai,qqpinglun,qqhuifu表,這是初始狀態(tài):
先以用戶李四登錄,由數(shù)據(jù)庫qqfriends表中知道,李四的好友是zhangsan, 和zhaoliu,那么他的空間中顯示的好友動態(tài)如下:
與上一篇相比,在這一篇中,誰登錄的我用中文顯示的:
<?php session_start(); $uid = ""; if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } $uid = $_SESSION["uid"]; require "../DB.class.php"; $db = new DB(); $sql = "select name from qqusers where uid='{$uid}'"; $name = $db->strquery($sql); echo "歡迎:"."<span class='qid' yh='{$uid}'>{$name}</span>"; ?>
第一步:評論
1、評論張三的動態(tài),點擊“確定”后,就是第二張圖了~
2、并將評論的內(nèi)容寫進數(shù)據(jù)庫
//定義空字符串,容納評論的id var code=""; $(".pl").click(function(){ code = $(this).attr("code"); //將評論的id重新賦值 }) //將評論寫進數(shù)據(jù)庫 $("#tjpl").click(function(){ var plnr = $(".pldt").val(); var plid = code; //取發(fā)動態(tài)的id $.ajax({ url:"pl-cl.php", data:{plnr:plnr,plid:plid}, type:"POST", dataType:"TEXT", success:function(data){ alert("評論成功!"); window.location.href="main.php" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); })
pl-cl.php頁面<br><br>
<?php require "../DB.class.php"; $db = new DB(); session_start(); $uid = $_SESSION["uid"]; $plnr = $_POST["plnr"]; $dtid = $_POST["plid"]; $time = date("Y-m-d H:i:s", time()); $sql = "insert into qqpinglun values ('','{$dtid}','{$uid}','{$plnr}','{$time}')"; $db->query($sql,0); ?>
查看qqpinglun表中是不是多了這一條 “為什么開心呢?”:
3、讀取評論內(nèi)容:
<!--讀取評論內(nèi)容--> <div id="dqpl"> <?php $sql = "select * from qqpinglun"; $arr = $db->query($sql); foreach($arr as $v) { $sql = "select * from qqdongtai where dtid='{$v[1]}'"; $arr2 = $db->query($sql); foreach($arr2 as $m) { //取發(fā)動態(tài)的姓名 $sql = "select name from qqusers where uid='{$v[2]}'"; $name = $db->strquery($sql); //若果是登錄者評論則顯示“我” if($v[2]==$uid) { $name ="我"; } //獲取被評論者的姓名 $sql = "select name from qqusers where uid=(select uid from qqdongtai where dtid='{$v[1]}')"; $bpl = $db->strquery($sql); echo "<div class='a'><span class='xm'>{$name}</span>評論<span class='xm'>{$bpl}</span>的動態(tài):{$m[2]}<div> <div class='b'>{$v[3]}</div> <div class='c'>發(fā)表評論時間:{$v[4]}</div> <div class='d'><button class='btn btn-primary hf' ids ='{$v[0]}'>回復(fù) </button><span><a href='scpl-cl.php?code={$v[0]}'>刪除評論</a></span></div>"; } } ?> </div>
第二步:回復(fù)
1、回復(fù)剛剛的評論:
2、將回復(fù)內(nèi)容寫進數(shù)據(jù)庫
//定義空字符串,容納回復(fù)評論的id var ids=""; $(".hf").click(function(){ ids = $(this).attr("ids"); //將評論的id重新賦值 // alert((ids)); $('#mM').modal('show'); }) //將回復(fù)評論寫進數(shù)據(jù)庫 $("#tjhf").click(function(){ var hfnr = $(".hfpl").val(); // alert(hfnr); // alert(ids); $.ajax({ url:"hf-cl.php", data:{hfnr:hfnr,ids:ids}, type:"POST", dataType:"TEXT", success:function(data){ alert("回復(fù)成功!"); window.location.href="main.php" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); })
hf-cl.php頁面
<?phprequire "../DB.class.php"; $db = new DB(); session_start(); $uid = $_SESSION["uid"]; $hfnr = $_POST["hfnr"]; $cid = $_POST["ids"]; $time = date("Y-m-d H:i:s", time()); $sql = "insert into qqhuifu values ('','{$cid}','{$uid}','{$hfnr}','{$time}')"; $db->query($sql,0); ?>
查看qqhuifu表,是不是多了一行呢?
3、將回復(fù)內(nèi)容讀出:
<div id="dqhf"> <!--取一次回復(fù)--> <?php $sql = "select * from qqhuifu where cid in (select cid from qqpinglun)"; $arr = $db->query($sql); foreach($arr as $a) { $sql = "select * from qqpinglun where cid='{$a[1]}'"; $arr2 = $db->query($sql); foreach($arr2 as $n) { //取評論動態(tài)的姓名 $sql = "select name from qqusers where uid='{$a[2]}'"; $name = $db->strquery($sql); //若果是登錄者評論則顯示“我” if($a[2]==$uid) { $name ="我"; } //獲取被回復(fù)評論的姓名 $sql = "select name from qqusers where uid=(select uid from qqpinglun where cid='{$a[1]}')"; $bpl = $db->strquery($sql); echo "<div class='a'><span class='xm'>{$name}</span>回復(fù)<span class='xm'>{$bpl}</span>的評論:{$n[3]}<div> <div class='b'>{$a[3]}</div> <div class='c'>回復(fù)時間:{$a[4]}</div> <div class='d'><button class='btn btn-primary hf' ids ='{$a[0]}'>回復(fù) </button><span><a href='schf-cl.php?code={$a[0]}'>刪除回復(fù)</a></span></div>"; } } ?> </div>
回復(fù)內(nèi)容已經(jīng)顯示了:
第三步:刪除
1、刪除動態(tài):(含評論和回復(fù))
scdt-cl.php
<?php $code = $_GET["code"]; require "../DB.class.php"; $db = new DB(); $sql = "delete from qqdongtai where dtid='{$code}'"; $db->query($sql,0); $sql2 = "delete from qqpinglun where dtid='{$code}'"; $db->query($sql2,0); $sql3 = "delete from qqhuifu where cid=(select cid from qqpinglun where dtid='{$code}')"; $db->query($sql3,0); header("location:main.php"); ?>
2、刪除評論:(含回復(fù))
scpl-cl.php
<?php $code = $_GET["code"]; require "../DB.class.php"; $db = new DB(); $sql2 = "delete from qqpinglun where cid='{$code}'"; $db->query($sql2,0); $sql3 = "delete from qqhuifu where cid='{$code}'"; $db->query($sql3,0); header("location:main.php"); ?>
3、刪除回復(fù):(只自己)
schf-cl.php
<?php $code = $_GET["code"]; require "../DB.class.php"; $db = new DB(); $sql2 = "delete from qqpinglun where cid='{$code}'"; $db->query($sql2,0); $sql3 = "delete from qqhuifu where cid='{$code}'"; $db->query($sql3,0); header("location:main.php"); ?>
關(guān)于刪除就不依次試了~~~注意包含關(guān)系就好了
主頁面全部代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <!--引入bootstrap的css文件--> <link type="text/css" rel="stylesheet" href="../bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="external nofollow" /> <!--引入js包--> <script src="../jquery-3.2.0.js"></script> <!--引入bootstrap的js文件--> <script src="../bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <style> #body{ height: auto; width: 1000px; margin: 0px auto; } #xdt{ height: 200px; width:1000px; border: 1px solid cadetblue; } /*動態(tài)和評論div*/ .fdt{ position: relative; width: 1000px; } /*讀取內(nèi)容div*/ #nr{ width: 1000px; } /*誰發(fā)表動態(tài)樣式*/ .a{ float: left; min-height:40px; width: 1000px; background-color: goldenrod; } /*姓名*/ .xm{ font-size: 18px; color: brown; font-weight: bold; } /*發(fā)表動態(tài)樣式內(nèi)容*/ .b{ float: left; text-align: left; height:100px; line-height: 50px; width: 100%; background-color: greenyellow; } /*發(fā)表時間與回復(fù)刪除樣式*/ .c{ height:30px; width: 800px; float: left; font-size: 12px; text-align:right; background-color: gainsboro; } /*彈出模態(tài)框評論框*/ .d{ height:30px; width: 200px; float: left; font-size: 15px; text-align:center; background-color: gainsboro; } /*讀取評論div*/ #dqpl{ width: 1000px; } #dqhf { width: 1000px; } </style> </head> <body> <div id="body"> <?php session_start(); $uid = ""; if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } $uid = $_SESSION["uid"]; require "../DB.class.php"; $db = new DB(); $sql = "select name from qqusers where uid='{$uid}'"; $name = $db->strquery($sql); //這種方法可以取到uid。 echo "歡迎:"."<span class='qid' yh='{$uid}'>{$name}</span>"; ?> <!--寫動態(tài)--> <div id="xdt"> <p>發(fā)表動態(tài):</p> <textarea cols="100px" rows="5px" name="xdt" class="xdt"></textarea> <input type="submit" value="發(fā)表" id="fb" /> </div> <!--動態(tài)內(nèi)容結(jié)束--> <!--容納動態(tài)內(nèi)容--> <div class="fdt"> <p >朋友動態(tài):<p> <!--讀取動態(tài)內(nèi)容--> <div id="nr"> <?php $date = date ("Y-m-d H:i:s"); $sql = "select * from qqdongtai where uid='{$uid}' or uid in (select uid from qqfriends where fname =(select name from qqusers where uid='{$uid}')) order by time desc"; //echo $sql; $arr = $db->query($sql); // var_dump($arr); foreach($arr as $v) { $sql = "select name from qqusers where uid='{$v[1]}'"; $name = $db->strquery($sql); if($v[1]==$uid) { $name = "我"; } echo "<div class='a'><span class='xm'>{$name}</span>發(fā)表動態(tài):</div> <div class='b'>{$v[2]}</div> <div class='c'>發(fā)表動態(tài)時間:{$v[3]}</div> <div class='d'><button class='btn btn-primary pl' data-toggle='modal' data-target='#myModal' code ='$v[0]'>評論</button> <span><a href='scdt-cl.php?code={$v[0]}'>刪除動態(tài)</a></span></div>"; } ?> </div> <!--讀取評論內(nèi)容--> <div id="dqpl"> <?php $sql = "select * from qqpinglun"; $arr = $db->query($sql); foreach($arr as $v) { $sql = "select * from qqdongtai where dtid='{$v[1]}'"; $arr2 = $db->query($sql); foreach($arr2 as $m) { //取發(fā)動態(tài)的姓名 $sql = "select name from qqusers where uid='{$v[2]}'"; $name = $db->strquery($sql); //若果是登錄者評論則顯示“我” if($v[2]==$uid) { $name ="我"; } //獲取被評論者的姓名 $sql = "select name from qqusers where uid=(select uid from qqdongtai where dtid='{$v[1]}')"; $bpl = $db->strquery($sql); echo "<div class='a'><span class='xm'>{$name}</span>評論<span class='xm'>{$bpl}</span>的動態(tài):{$m[2]}<div> <div class='b'>{$v[3]}</div> <div class='c'>發(fā)表評論時間:{$v[4]}</div> <div class='d'><button class='btn btn-primary hf' ids ='{$v[0]}'>回復(fù) </button><span><a href='scpl-cl.php?code={$v[0]}'>刪除評論</a></span></div>"; } } ?> </div> <!--讀取回復(fù)的內(nèi)容--> <div id="dqhf"> <?php $sql = "select * from qqhuifu where cid in (select cid from qqpinglun)"; $arr = $db->query($sql); // var_dump($arr); foreach($arr as $a) { $sql = "select * from qqpinglun where cid='{$a[1]}'"; $arr2 = $db->query($sql); // var_dump($arr2); foreach($arr2 as $n) { //取評論動態(tài)的姓名 $sql = "select name from qqusers where uid='{$a[2]}'"; $name = $db->strquery($sql); //若果是登錄者評論則顯示“我” if($a[2]==$uid) { $name ="我"; } //獲取被回復(fù)評論的姓名 $sql = "select name from qqusers where uid=(select uid from qqpinglun where cid='{$a[1]}')"; $bpl = $db->strquery($sql); echo "<div class='a'><span class='xm'>{$name}</span>回復(fù)<span class='xm'>{$bpl}</span>的評論:{$n[3]}<div> <div class='b'>{$a[3]}</div> <div class='c'>回復(fù)時間:{$a[4]}</div> <div class='d'><button class='btn btn-primary hf' ids ='{$a[0]}'>回復(fù) </button><span><a href='schf-cl.php?code={$a[0]}'>刪除回復(fù)</a></span></div>"; } } ?> </div> </div> <!-- 評論模態(tài)框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h5 class="modal-title" id="myModalLabel">評論</h5> </div> <textarea class="modal-body pldt" cols="80px"></textarea> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button> <button type="button" class="btn btn-primary" id="tjpl">提交評論</button> </div> </div> </div> </div> <!--模態(tài)框結(jié)束--> <!-- 回復(fù)模態(tài)框(Modal) --> <div class="modal fade" id="mM" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h5 class="modal-title" id="myModalLabel">回復(fù)</h5> </div> <textarea class="modal-body hfpl" cols="80px"></textarea> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button> <button type="button" class="btn btn-primary" id="tjhf">提交回復(fù)</button> </div> </div> </div> </div> <!--模態(tài)框結(jié)束--> </div> </body> </html> <script> //ajax方法:刷新頁面時將內(nèi)容讀取出來,并按發(fā)表時間讀出來 // $.ajax({ // url:"sx-cl.php", // dataType:"TEXT", // success:function(data){ // var hang = data.trim().split("|"); // var str=""; // for(var i=0;i<hang.length;i++) // { // var lie = hang[i].split("^"); // str = str + "<div class='a'><span class='xm'>"+lie[1]+"</span>發(fā)表動態(tài):</div><div class='b'><p>"+lie[2]+"</p><div class='c'>發(fā)表動態(tài)時間:"+lie[3]+"</div>"; // str =str+"<div id='d'><button class='btn btn-primary pl' data-toggle='modal' data-target='#myModal' code ='"+lie[0]+"'>評論</button><span><a href='del.php?code="+lie[0]+"'>刪除動態(tài)</a></span></div>"; // } // $("#nr").html(str); // //點擊“評論按鈕”實現(xiàn)將code值傳到模態(tài)框的“提交按鈕” // //為什么放在此處:因為ajax是異步的,如果不放在此處會加不上點擊事件 // $(".pl").click(function(){ // code = $(this).attr("code"); //將評論的id重新賦值 // }) // } // }); // //php方法: 當(dāng)發(fā)表動態(tài)時,將動態(tài)內(nèi)容寫進數(shù)據(jù)庫,并刷新頁面 $("#fb").click(function(){ var dt= $(".xdt").val(); var uid = $(".qid").attr("yh"); $.ajax({ url:"main-cl.php", data:{dt:dt}, type:"POST", dataType:"TEXT", success:function(data){ alert("發(fā)表動態(tài)成功!"); window.location.href="main.php" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) //定義空字符串,容納評論的id var code=""; $(".pl").click(function(){ code = $(this).attr("code"); //將評論的id重新賦值 }) //將評論寫進數(shù)據(jù)庫 $("#tjpl").click(function(){ var plnr = $(".pldt").val(); var plid = code; //取發(fā)動態(tài)的id // alert(plnr); // alert(plid); $.ajax({ url:"pl-cl.php", data:{plnr:plnr,plid:plid}, type:"POST", dataType:"TEXT", success:function(data){ alert("評論成功!"); window.location.href="main.php" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) //定義空字符串,容納回復(fù)評論的id var ids=""; $(".hf").click(function(){ ids = $(this).attr("ids"); //將評論的id重新賦值 // alert((ids)); $('#mM').modal('show'); }) //將回復(fù)評論寫進數(shù)據(jù)庫 $("#tjhf").click(function(){ var hfnr = $(".hfpl").val(); // alert(hfnr); // alert(ids); $.ajax({ url:"hf-cl.php", data:{hfnr:hfnr,ids:ids}, type:"POST", dataType:"TEXT", success:function(data){ alert("回復(fù)成功!"); window.location.href="main.php" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) </script>
到此處為止,動態(tài)的發(fā)布、動態(tài)的評論、動態(tài)的回復(fù)、動態(tài)的刪除都已經(jīng)寫完了,但是有個問題還還還沒解決完,也就是回復(fù)的回復(fù)問題。請看下面的簡圖:
也就是回復(fù)表中有一部分是回復(fù)的評論,而剩余的部分則是回復(fù)的回復(fù)(有點繞)想看的就繼續(xù)關(guān)注(下)未完待續(xù)~~~
先總結(jié)一下遇到的問題:
(1)為什么ajax輸出的button添加不上點擊事件?
因為ajax是異步ajax,所以要緊接其后。
(2)為什么取不到button的值------this
(3)一個php頁面中,什么時候用ajax?什么時候用php?
在這個實例中,我用ajax將數(shù)據(jù)寫進數(shù)據(jù)庫;用php從數(shù)據(jù)庫讀取內(nèi)容。(上一篇中,動態(tài)是用ajax讀取的,在這一篇中,兩種方法都有,詳情請看全部代碼)
(4)最后,邏輯清晰很關(guān)鍵,尤其是表與表之間的關(guān)聯(lián)。
關(guān)于利用php怎么一個評論回復(fù)和動態(tài)刪除功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。