Home Backend Development PHP Tutorial PHP imitates QQ space to realize its functional examples

PHP imitates QQ space to realize its functional examples

Jun 15, 2017 pm 02:29 PM
php qq space

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles