This article mainly introduces the functions of imitating QQ space or circle of friends to publish updates, comment on updates, reply to comments, delete updates or comments (middle), friends in need can refer to the following
in the previous article The essay has already presented how to publish updates, so now let’s take a look at the remaining comment updates, reply comments, delete updates and comment functions. These functions are a bit confusing~~~
1. The idea is as follows:
(1) After you post a post, someone will comment on this post. After commenting, you will also reply to the comment; (comments here must be A separate table, and a separate table for replies)
(2) Delete updates: all updates, comments, and replies will be deleted; delete comments: only the comment will be deleted
2. Before writing the code, I still want to talk about the process:
(1) Post updates---comments---reply---again Reply
(2) To refine the above process, I first wrote it on paper and then uploaded it. The code words cannot express it clearly (note that the implementation of the functions I want is not exactly the same Ha)
## Third, let’s explain the code in chunks first, and finally attach the main page code completely (Including the previous article)
In the previous article, we have implemented the posting of updates and pop-up comment boxes, so now go down: Look at qqfriends separately, qqdongtai, qqpinglun, qqhuifu table, this is the initial state:## First log in as user Li Si. From the qqfriends table in the database, we know that Li Si’s friends are zhangsan, and zhaoliu. Then the friend dynamics displayed in his space are as follows:
Compared with the previous article, in this article, I display who logged in in Chinese:
strquery($sql); echo "欢迎:"."{$name}"; ?>
1. Comment on Zhang San’s updates. After clicking “OK”, it will be the second picture~
2. And write the content of the comment into the database
//定义空字符串,容纳评论的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" ; } }); })
query($sql,0); ?>
3. Read the comment content:
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]}
"; } } ?>
1. Reply to the comment just now:
2. Write the reply content into the database
//定义空字符串,容纳回复评论的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" ; } }); })
query($sql,0); ?>
3. Read the reply content:
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 "
{$name}回复{$bpl}的评论:{$n[3]}
{$a[3]}
回复时间:{$a[4]}
"; } } ?>
Step 3: Delete
1. Delete updates: (including comments and replies)
scdt-cl.php
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"); ?>
scpl-cl.php
query($sql2,0); $sql3 = "delete from qqhuifu where cid='{$code}'"; $db->query($sql3,0); header("location:main.php"); ?>
schf-cl.php
query($sql2,0); $sql3 = "delete from qqhuifu where cid='{$code}'"; $db->query($sql3,0); header("location:main.php"); ?>
All codes for the main page:
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]}
"; } } ?>评论
回复
That is to say, part of the reply table is the reply comments, and the remaining part is the reply reply (a bit convoluted) If you want to see it, continue to pay attention (below) To be continued ~~~
Let’s first summarize the problems encountered:
(1) Why can’t the button output by ajax add a click event?
Because ajax is asynchronous ajax, it must be followed immediately.
(2) Why can’t I get the value of button------this
(3) When to use ajax in a php page? When to use php?
In this example, I use ajax to write data into the database; I use php to read content from the database. (In the previous article, the dynamics were read using ajax. In this article, both methods are available. Please see the entire code for details)
(4) Finally, clear logic is crucial, especially Associations between tables.
The above is the detailed content of PHP imitates QQ space to realize its functional examples. For more information, please follow other related articles on the PHP Chinese website!