PHP imitates QQ space to realize its functional examples

零下一度
Release: 2023-03-10 12:24:01
Original
3898 people have browsed it

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:

<?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=&#39;{$uid}&#39;";
   $name = $db->strquery($sql);
   echo "欢迎:"."<span class=&#39;qid&#39; yh=&#39;{$uid}&#39;>{$name}</span>";
   ?>
Copy after login

Step One: Comment

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" ;
  }      
  });            
 })
Copy after login

pl-cl.php page

<?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 (&#39;&#39;,&#39;{$dtid}&#39;,&#39;{$uid}&#39;,&#39;{$plnr}&#39;,&#39;{$time}&#39;)";
$db->query($sql,0);
?>
Copy after login

Check if there is an additional item "Why are you happy?" in the qqpinglun table:

3. Read the comment content:

<!--读取评论内容--> 
<p id="dqpl">
<?php
 $sql = "select * from qqpinglun";
 $arr = $db->query($sql);
 foreach($arr as $v)
 {
  $sql = "select * from qqdongtai where dtid=&#39;{$v[1]}&#39;";
  $arr2 = $db->query($sql);
  foreach($arr2 as $m)
  {
   //取发动态的姓名
   $sql = "select name from qqusers where uid=&#39;{$v[2]}&#39;";
   $name = $db->strquery($sql);
   //若果是登录者评论则显示“我”
   if($v[2]==$uid)
   {
    $name ="我";
   }
   //获取被评论者的姓名
   $sql = "select name from qqusers where uid=(select uid from qqdongtai where dtid=&#39;{$v[1]}&#39;)";
   $bpl = $db->strquery($sql);
   echo "<p class=&#39;a&#39;><span class=&#39;xm&#39;>{$name}</span>评论<span class=&#39;xm&#39;>{$bpl}</span>的动态:{$m[2]}<p>
    <p class=&#39;b&#39;>{$v[3]}</p>
   <p class=&#39;c&#39;>发表评论时间:{$v[4]}</p>
   <p class=&#39;d&#39;><button class=&#39;btn btn-primary hf&#39; ids =&#39;{$v[0]}&#39;>回复
   </button><span><a href=&#39;scpl-cl.php?code={$v[0]}&#39;>删除评论</a></span></p>";
  }
 }  
?>    
</p>
Copy after login

Step 2: Reply

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)); 
       $(&#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" ;
     }      
     });            
    })
Copy after login

hf-cl.php 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 (&#39;&#39;,&#39;{$cid}&#39;,&#39;{$uid}&#39;,&#39;{$hfnr}&#39;,&#39;{$time}&#39;)";
$db->query($sql,0);
?>
Copy after login

Check the qqhuifu table. Is there an extra row?

3. Read the reply content:

<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=&#39;{$a[1]}&#39;";
     $arr2 = $db->query($sql);
     foreach($arr2 as $n)
     {
      //取评论动态的姓名
      $sql = "select name from qqusers where uid=&#39;{$a[2]}&#39;";
      $name = $db->strquery($sql);
      //若果是登录者评论则显示“我”
      if($a[2]==$uid)
      {
       $name ="我";
      }
      //获取被回复评论的姓名
      $sql = "select name from qqusers where uid=(select uid from qqpinglun where cid=&#39;{$a[1]}&#39;)";
      $bpl = $db->strquery($sql);
      echo "<p class=&#39;a&#39;><span class=&#39;xm&#39;>{$name}</span>回复<span class=&#39;xm&#39;>{$bpl}</span>的评论:{$n[3]}<p>
       <p class=&#39;b&#39;>{$a[3]}</p>
      <p class=&#39;c&#39;>回复时间:{$a[4]}</p>
      <p class=&#39;d&#39;><button class=&#39;btn btn-primary hf&#39; ids =&#39;{$a[0]}&#39;>回复
      </button><span><a href=&#39;schf-cl.php?code={$a[0]}&#39;>删除回复</a></span></p>"; 
     }
     }
    ?>
   </p>
Copy after login

The reply content has been displayed:

Step 3: Delete

1. Delete updates: (including comments and replies)

scdt-cl.php

<?php
$code = $_GET["code"];
require "../DB.class.php";
$db = new DB();
$sql = "delete from qqdongtai where dtid=&#39;{$code}&#39;";
$db->query($sql,0);
$sql2 = "delete from qqpinglun where dtid=&#39;{$code}&#39;";
$db->query($sql2,0);
$sql3 = "delete from qqhuifu where cid=(select cid from qqpinglun where dtid=&#39;{$code}&#39;)";
$db->query($sql3,0);
header("location:main.php");
?>
Copy after login

2. Delete comment: (including reply)

scpl-cl.php

<?php
$code = $_GET["code"];
require "../DB.class.php";
$db = new DB();
$sql2 = "delete from qqpinglun where cid=&#39;{$code}&#39;";
$db->query($sql2,0);
$sql3 = "delete from qqhuifu where cid=&#39;{$code}&#39;";
$db->query($sql3,0);
header("location:main.php");
?>
Copy after login
Copy after login

3. Delete reply: (Only yourself)

schf-cl.php

<?php
$code = $_GET["code"];
require "../DB.class.php";
$db = new DB();
$sql2 = "delete from qqpinglun where cid=&#39;{$code}&#39;";
$db->query($sql2,0);
$sql3 = "delete from qqhuifu where cid=&#39;{$code}&#39;";
$db->query($sql3,0);
header("location:main.php");
?>
Copy after login
Copy after login

Regarding deletion, I won’t try it one by one~~~Just pay attention to the inclusion relationship

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]}

删除回复

"; } } ?>

<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=&#39;a&#39;><span class=&#39;xm&#39;>"+lie[1]+"</span>发表动态:</p><p class=&#39;b&#39;><p>"+lie[2]+"</p><p class=&#39;c&#39;>发表动态时间:"+lie[3]+"</p>"; // str =str+"<p id=&#39;d&#39;><button class=&#39;btn btn-primary pl&#39; data-toggle=&#39;modal&#39; data-target=&#39;#myModal&#39; code =&#39;"+lie[0]+"&#39;>评论</button><span><a href=&#39;del.php?code="+lie[0]+"&#39;>删除动态</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=&quot;&quot;; $(&quot;.hf&quot;).click(function(){ ids = $(this).attr(&quot;ids&quot;); //将评论的id重新赋值 // alert((ids)); $(&amp;#39;#mM&amp;#39;).modal(&amp;#39;show&amp;#39;); }) //将回复评论写进数据库 $(&quot;#tjhf&quot;).click(function(){ var hfnr = $(&quot;.hfpl&quot;).val(); // alert(hfnr); // alert(ids); $.ajax({ url:&quot;hf-cl.php&quot;, data:{hfnr:hfnr,ids:ids}, type:&quot;POST&quot;, dataType:&quot;TEXT&quot;, success:function(data){ alert(&quot;回复成功!&quot;); window.location.href=&quot;main.php&quot; rel=&quot;external nofollow&quot; rel=&quot;external nofollow&quot; rel=&quot;external nofollow&quot; rel=&quot;external nofollow&quot; rel=&quot;external nofollow&quot; ; } }); }) </script>
Copy after login

Up to this point, dynamic publishing, dynamic comments, dynamic replies, and dynamic deletions have all been written, but There is a question that has not yet been resolved, which is the question of replies. Please look at the diagram below:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!