Php imitates the functions (middle) of the QQ space or circle of friends to publish, comment, reply to, or delete dynamic or comments. php circle of friends
How to publish and dynamically present the content in the previous article. Now let's take a look at the remaining comment dynamics, reply to comments, delete dynamic content, and comment functions. These functions will be a bit different ~~~
I. The idea is as follows:
(1) When you post a dynamic review, someone will comment on the dynamic review. After the review, you will also reply to the comment. (a separate table is required for the comment here, A separate table is required for the reply)
(2) Delete dynamic: All dynamic comments and replies will be deleted; Delete comment: only this comment will be deleted.
2. Before writing the code, I still want to explain the process again:
(1) publish a dynamic post-comment-reply
(2) to refine the above process, I first write it on paper and then upload it. The code word cannot be clearly expressed (note that the implementation of the function I want, not exactly)
3. Explain the code in blocks first, and then complete the code on the home page (including the previous article)
In the previous article, we have implemented Release dynamics and a pop-up comment box. Now let's go down:
Take a look at the qqfriends, qqdongtai, qqpinglun, and qqhuifu tables respectively. This is the initial status:
Log on to the user Li Si, as shown in the QQ friends table of the database. The Friends of Li Si are zhangsan and zhaoliu. Then, the dynamics of friends displayed in his space are as follows:
Compared with the previous article, in this article, I used Chinese characters to show who logged on:
<? 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 "Welcome :". "<span class = 'qid' yh = '{$ uid}'> {$ name} </span>";?>
Step 1: Comment
1. Comment on James's dynamics. Click "OK", and the second figure is displayed ~
2. Write the comments into the database.
// Define an empty string that contains the comment id var code = ""; $ (". pl "). click (function () {code = $ (this ). attr ("code"); // revalue the comment id}) // write the comment into the database $ ("# tjpl "). click (function () {var plnr = $ (". pldt "). val (); var plid = code; // obtain the dynamic id $. ajax ({url: "pl-cl.php", data: {plnr: plnr, plid: plid}, type: "POST", dataType: "TEXT", success: function (data) {alert ("comment successful! "); Window. location. href = "main. php "rel =" external nofollow "rel =" external nofollow ";}});})
Pl-cl.php pages <br>
<?phprequire "../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);?>
Check if the qqpinglun table contains this "why are you happy ?" :
3. read comments:
<! -- Read comments --> <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) {// obtain the dynamic name $ SQL = "select name from qqusers where uid = '{$ v [2]}'"; $ name = $ db-> strquery ($ SQL); // if the comment is sent, "I" is displayed. if ($ v [2] = $ uid) {$ name = "I";} // obtain the name of the reviewer $ SQL = "select name from qqusers where uid = (select uid fr Om qqdongtai where dtid = '{$ v [1]}') "; $ bpl = $ db-> strquery ($ SQL ); echo "<div class = 'A'> <span class = 'xm '>{$ name} </span> comments <span class = 'xm' >{$ bpl} </span>: {$ m [2]} <div> <div class = 'B' >{$ v [3]} </div> <div class = 'C'> comment time: {$ v [4]} </div> <div class = 'D'> <button class = 'btn btn-primary hf 'IDs = '{$ v [0]} '> reply </button> <span> <a href = 'scpl-cl. php? Code = {$ v [0]} '> delete comments </a> </span> </div> ";}}?> </Div>
Step 2: Reply
1. Reply to the previous comment:
2. Write the reply content into the database
// Define an empty string that contains the id var ids = ""; $ (". hf "). click (function () {ids = $ (this ). attr ("ids"); // revalue the comment id // alert (ids); $ ('# mm '). modal ('show');}) // write the reply comment to the database $ ("# 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 ("Reply successful! "); Window. location. href = "main. php "rel =" external nofollow "rel =" external nofollow ";}});})
Hf-cl.php web page
<?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);?>
Check the qqhuifu table. Is there an additional row?
3. Read the reply content:
<Div id = "dqhf"> <! -- Get a reply --> <? 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) {// retrieve the dynamic comment name $ SQL = "select name from qqusers where uid = '{$ a [2]}'"; $ name = $ db-> strquery ($ SQL); // if the comment is sent, "I" is displayed. if ($ a [2] = $ uid) {$ name = "I" ;}// get the name of the comment replied $ SQL = "select na Me 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> reply <span class = 'xm' >{$ bpl} </span> comments: {$ n [3]} <div> <div class = 'B' >{$ a [3]} </div> <div class = 'C'> reply time: {$ a [4]} </div> <div class = 'D'> <button class = 'btn btn-primary hf 'IDs = '{$ a [0]} '> reply </button> <span> <a href = 'schf-cl. php? Code = {$ a [0]} '> delete a reply </a> </span> </div> ";}}?> </Div>
The reply content is displayed as follows:
Step 3: delete
1. Delete dynamic content (including comments and replies)
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. delete comments: (including replies)
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. Delete the reply: (only yourself)
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");?>
I will not try to delete it in sequence ~~~ Pay attention to the inclusion relationship.
All code on the home page:
<! DOCTYPE html>
So far, dynamic publishing, dynamic comments, dynamic replies, and deletion have been completed, but a problem has not been solved yet, that is, the reply question. See the following figure:
That is, some of the replies in the table are reply comments, while the remaining part is the reply (a bit around). If you want to see the reply, continue to follow up (below ~~~
Summarize the problems:
(1) Why cannot I add a click event to the button output by ajax?
Because ajax is asynchronous ajax, it must be followed immediately.
(2) Why can't we get the value of the button ------ this
(3) When Will ajax be used on a php page? When Will php be used?
In this example, I use ajax to write data into the database, and use php to read content from the database. (In the previous article, the dynamic data was read using ajax. In this article, both methods are available. For details, see all the code)
(4) Finally, logic clarity is critical, especially the association between tables.