這篇文章主要介紹了模仿qq空間或朋友圈發布動態、評論動態、回覆評論、刪除動態或評論的功能(中) ,需要的朋友可以參考下
在上一篇隨筆中已經將如何發布動態呈現了,那麼現在來看看剩下的評論動態、回覆評論、刪除動態和評論功能,這幾個功能會有點繞~~~
一、思路如下:
(1)你發表動態之後,會有人評論這一動態,當評論之後,你也會回覆該評論;(此處評論要單獨一張表,回覆也要單獨一張表)
(2)刪除動態:會將動態連同評論、回覆全部刪除;刪除評論:只會刪除該條評論
二、在寫程式碼之前,我還是想把流程說一遍:
#(1)發表動態---評論---回覆---再回覆
(2)將上邊的流程細化,我先在紙上寫出,再上傳,碼字不能表達清楚(注意的是,我想要的功能的實現,並不是一模一樣的哈)
#三、還是先將程式碼分塊解釋,最後將主頁碼完全附上(含上一篇)
在上一篇中已經實現發布動態、彈出評論框,那麼現在接著向下走:
分別看一下qqfriends, qqdongtai,qqpinglun,qqhuifu表,這是初始狀態:
#先以用戶李四登錄,由資料庫qqfriends表中知道,李四的好友是zhangsan, 和zhaoliu,那麼他的空間中顯示的好友動態如下:
#與上一篇相比,在這篇文章中,誰登入的我用中文顯示的:
<?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、評論張三的動態,點選「確定」後,就是第二張圖了~
##2、並將評論的內容寫進資料庫//定义空字符串,容纳评论的id var code=""; $(".pl").click(function(){ code = $(this).attr("code"); //将评论的id重新赋值 }) //将评论写进数据库 $("#tjpl").click(function(){ var plnr = $(".pldt").val(); var plid = code; //取发动态的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" ; } }); })
<?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); ?>
3、讀取評論內容:
<!--读取评论内容--> <p 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) { //取发动态的姓名 $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 "<p class='a'><span class='xm'>{$name}</span>评论<span class='xm'>{$bpl}</span>的动态:{$m[2]}<p> <p class='b'>{$v[3]}</p> <p class='c'>发表评论时间:{$v[4]}</p> <p class='d'><button class='btn btn-primary hf' ids ='{$v[0]}'>回复 </button><span><a href='scpl-cl.php?code={$v[0]}'>删除评论</a></span></p>"; } } ?> </p>
2、將回覆內容寫進資料庫
//定义空字符串,容纳回复评论的id var ids=""; $(".hf").click(function(){ ids = $(this).attr("ids"); //将评论的id重新赋值 // alert((ids)); $('#mM').modal('show'); }) //将回复评论写进数据库 $("#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("回复成功!"); window.location.href="main.php" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); })
<?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); ?>
<p id="dqhf"> <!--取一次回复--> <?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) { //取评论动态的姓名 $sql = "select name from qqusers where uid='{$a[2]}'"; $name = $db->strquery($sql); //若果是登录者评论则显示“我” if($a[2]==$uid) { $name ="我"; } //获取被回复评论的姓名 $sql = "select name from qqusers where uid=(select uid from qqpinglun where cid='{$a[1]}')"; $bpl = $db->strquery($sql); echo "<p class='a'><span class='xm'>{$name}</span>回复<span class='xm'>{$bpl}</span>的评论:{$n[3]}<p> <p class='b'>{$a[3]}</p> <p class='c'>回复时间:{$a[4]}</p> <p class='d'><button class='btn btn-primary hf' ids ='{$a[0]}'>回复 </button><span><a href='schf-cl.php?code={$a[0]}'>删除回复</a></span></p>"; } } ?> </p>
回覆內容已經顯示了:
第三步:刪除
###1、刪除動態:(含註解與回覆)######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"); ?>
<?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"); ?>
<?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"); ?>
strquery($sql); //这种方法可以取到uid。 echo "欢迎:"."{$name}"; ?>
发表动态:
朋友动态:
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 "
{$name}发表动态:
{$v[2]}
发表动态时间:{$v[3]}
"; } ?>query($sql); foreach($arr as $v) { $sql = "select * from qqdongtai where dtid='{$v[1]}'"; $arr2 = $db->query($sql); foreach($arr2 as $m) { //取发动态的姓名 $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 "
{$name}评论{$bpl}的动态:{$m[2]}
{$v[3]}
发表评论时间:{$v[4]}
"; } } ?>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) { //取评论动态的姓名 $sql = "select name from qqusers where uid='{$a[2]}'"; $name = $db->strquery($sql); //若果是登录者评论则显示“我” if($a[2]==$uid) { $name ="我"; } //获取被回复评论的姓名 $sql = "select name from qqusers where uid=(select uid from qqpinglun where cid='{$a[1]}')"; $bpl = $db->strquery($sql); echo "
{$name}回复{$bpl}的评论:{$n[3]}
{$a[3]}
回复时间:{$a[4]}
"; } } ?>
评论
回复
<script> //ajax方法:刷新页面时将内容读取出来,并按发表时间读出来 // $.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 + "<p class='a'><span class='xm'>"+lie[1]+"</span>发表动态:</p><p class='b'><p>"+lie[2]+"</p><p class='c'>发表动态时间:"+lie[3]+"</p>"; // str =str+"<p 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]+"'>删除动态</a></span></p>"; // } // $("#nr").html(str); // //点击“评论按钮”实现将code值传到模态框的“提交按钮” // //为什么放在此处:因为ajax是异步的,如果不放在此处会加不上点击事件 // $(".pl").click(function(){ // code = $(this).attr("code"); //将评论的id重新赋值 // }) // } // }); // //php方法: 当发表动态时,将动态内容写进数据库,并刷新页面 $("#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("发表动态成功!"); 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重新赋值 }) //将评论写进数据库 $("#tjpl").click(function(){ var plnr = $(".pldt").val(); var plid = code; //取发动态的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" ; } }); }) //定义空字符串,容纳回复评论的id var ids=""; $(".hf").click(function(){ ids = $(this).attr("ids"); //将评论的id重新赋值 // alert((ids)); $(&#39;#mM&#39;).modal(&#39;show&#39;); }) //将回复评论写进数据库 $("#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("回复成功!"); window.location.href="main.php" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } }); }) </script>
先總結遇到的問題:
(1)為什麼ajax輸出的button新增不點選事件?
因為ajax是非同步ajax,所以要緊接在後。
(2)為什麼取不到button的值------this
(3)一個php頁面中,什麼時候用ajax?什麼時候用php?
在這個實例中,我用ajax將資料寫進資料庫;用php從資料庫讀取內容。 (上一篇中,動態是用ajax讀取的,在這篇中,兩種方法都有,詳情請看全部程式碼)
(4)最後,邏輯清晰很關鍵,尤其是表與表之間的關聯。
以上是php模仿qq空間實現其功能實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!