Home > Backend Development > PHP Tutorial > PHP example - PHP imitates the function of posting updates, commenting on updates, replying to comments, deleting updates or comments in QQ space or Moments (Part 1)

PHP example - PHP imitates the function of posting updates, commenting on updates, replying to comments, deleting updates or comments in QQ space or Moments (Part 1)

微波
Release: 2023-03-11 18:42:01
Original
2542 people have browsed it

Most of us have posted updates, and we must all know the entire process of posting updates, replying to comments, and deleting updates. So how is this function implemented? The editor below has brought you example code. Friends who are interested in PHP's functions of imitating QQ space or Moments to publish updates, comment on updates, reply to comments, delete updates or comments, let's learn together

We are big Some people have posted updates, and they must all know the entire process of posting updates, replying to comments, and deleting updates. As a beginner, it is a bit complicated to imitate these functions. At the very least, the relationship between tables must be clarified~~

Let’s sort out the idea first:

(1) User login, use session to read the current user----the purpose is: The user can post updates, and the focus is to display the updates posted by the user's friends and himself, sorted by publication time.

(2) Make a post dynamic box to realize the function of posting updates

(3) Display the dynamic information that the user and his friends have posted, and display them from nearest to far according to the posting time

(4) Make a comment button and delete button after each update; implement comments, replies and deletions on the updates (the next essay in italics, otherwise it will be too long)

Needed tables:

(1) User table:

(2) Friends table

(3) Dynamic table

I first parse the code in chunks, and finally attach the complete home page code. Otherwise, it may be a bit confusing if you don’t understand the logic~~~~

Step 1: Implement a simple login

(1 ) login.php page

<meta charset="UTF-8">
<title></title>
<style>
 #body{
  height: 300px;
  width: 300px;
  margin: 200px auto;
   
 }
</style>
<p id="body">
<form method="post" action="login-cl.php">
 用户名:<input type="text" name="uid"><br><br>
 密码:<input type="password" name="pwd"><br>
 <input type="submit" value="登录">
</form>
</p>
Copy after login

The rendering is as follows:

(2) login-cl.php page: (Use session to access users Name)

<!--?php
session_start();
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];
require "../DB.class.php";
$db = new DB();
$sql = "select pwd from users where uid = &#39;{$uid}&#39;";
$mm = $db--->strquery($sql);
var_dump($mm);
if($mm == $pwd && !empty($pwd))
{
 $_SESSION["uid"] = $uid;
 header("location:main.php");
}
else{
 echo "用户名或密码错误!";
}
?>
Copy after login

Step 2: After logging in, layout the publishing dynamic box

(1) Before publishing, determine whether the session has been retrieved to the value. If not, return to the login page. If the value is obtained, the font of "Welcome, xx" will be displayed (the following names are displayed in pinyin, and the names in Chinese characters will no longer be read)

<!--?php
   session_start();
   $uid = "";
   if(empty($_SESSION["uid"]))
   {
    header("location:login.php");
    exit;
   }
   $uid = $_SESSION["uid"];
   echo "欢迎:"."<span class=&#39;qid&#39; yh=&#39;{$uid}&#39;-->{$uid}";
   ?>
Copy after login

(2 )

<!--写动态-->
 <p id="xdt">
  <p>发表动态:</p>  
  <textarea cols="100px" rows="5px" name="xdt" class="xdt"></textarea>
  <input type="submit" value="发表" id="fb">
 </p>
Copy after login

Achieved effect:

Step 3: Display the dynamic information published by the user and his friends, and sort them from the most recent to the most recent publication time Remote display

The key points are:

(1) The displayed dynamics are only those of the logged-in user and his friends, non-friends will not be displayed - -------So when processing the sql statement on the page, you should pay attention

(2) Read the read information according to the publication time, the latest publication time is at the top

First:

<!--容纳动态内容--> 
  <p class="fdt">
   <p style="color: brown; font-family: &#39;微软雅黑&#39;;font-weight: bold;font-size: 20px; margin-bottom: 20px; margin-top: 20px;">朋友动态:</p><p>
   </p><p id="nr"></p>
  </p>
Copy after login

Secondly:

//当发表动态时,将动态内容写进数据库,并刷新页面
    $("#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" ;
     } 
    });
    })
Copy after login

Corresponding main-cl.php page:

<!--?php
session_start();
$uid = $_SESSION["uid"];
$dt = $_POST["dt"];
$date = date ("Y-m-d H:i:s");
require "../DB.class.php";
$db = new DB();
 $sql = "insert into qqdongtai values (&#39;&#39;,&#39;{$uid}&#39;,&#39;{$dt}&#39;,&#39;{$date}&#39;)";
 $db--->query($sql,0);
 $sql = "select * from qqdongtai where uid=&#39;{$uid}&#39; or uid in (select uid from qqfriends where fname =(select name from qqusers where uid=&#39;{$uid}&#39;))";
 //echo $sql;
 $arr = $db->strquery($sql);
 echo $arr;
?>
Copy after login

Then:

//刷新页面时将内容读取出来,并按发表时间读出来
    $.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 class="b">"+lie[2]+"<p></p><p class="c">发表动态时间:"+lie[3]+"</p>";              
       str =str+"<p id="d"><button class="btn btn-primary" data-toggle="modal" data-target="#myModal">评论</button><span><a href="del.php?code=" rel="external nofollow" +lie[0]+"">删除动态</a></span></p>";
      }
      $("#nr").html(str);
        
     } 
    });
</p></hang.length;i++)>
Copy after login

sx-cl.php page:

<!--?php
session_start();
$uid = $_SESSION["uid"];
$date = date ("Y-m-d H:i:s");
require "../DB.class.php";
$db = new DB();
//选取该用户和该用户好友的动态,并按时间顺训读出
 $sql = "select * from qqdongtai where uid=&#39;{$uid}&#39; or uid in (select uid from qqfriends where fname =(select name from qqusers where uid=&#39;{$uid}&#39;)) order by time desc";
 //echo $sql;
 $arr = $db--->strquery($sql);
 echo $arr;
?>
Copy after login

It can be seen from the above that the logged-in user is lisi. From the friend table, we can know that the only friends of lisi are zhangsan and zhaoliu, so the displayed dynamics can only be those of lisi, zhangsan, and zhaoliu. Now take a look at the effect and database~~~~

##Step 4: Use bootstrap to add a modal box for comments Dynamic

(1) Import file:

 <!--引入bootstrap的css文件-->
<link type="text/css" rel="stylesheet" href="../bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" >
<!--引入js包-->
<!--引入bootstrap的js文件-->
Copy after login

(2) Use modal box to make comments:

<!-- 评论模态框(Modal) -->
   <p class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <p class="modal-dialog">
     <p class="modal-content">
      <p class="modal-header">
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
       <h4 class="modal-title" id="myModalLabel">评论</h4>
      </p>
      <textarea class="modal-body" cols="80px"></textarea>
      <p class="modal-footer">
       <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
       <button type="button" class="btn btn-primary qdhf">确定</button>
      </p>
     </p><!-- /.modal-content -->
    </p><!-- /.modal -->
   </p>
Copy after login

Implementation effect: (Style comparison Simple)

Click "Comment":

At this step, you can basically achieve dynamic publishing and display of friends Updated~~~~To be continued----See the next essay for comments and comments reply~~~

All codes of the main page:



 
  
  
   
  
  
  
  
  
      
 
 
  

{$uid}"; ?>

发表动态:

朋友动态:

<script> //刷新页面时将内容读取出来,并按发表时间读出来 $.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;>"+lie[2]+"</p><p class=&#39;c&#39;>发表动态时间:"+lie[3]+"</p>"; str =str+"<p id=&#39;d&#39;><button class=&#39;btn btn-primary&#39; data-toggle=&#39;modal&#39; data-target=&#39;#myModal&#39;>评论</button><span><a href=&#39;del.php?code="+lie[0]+"&#39;>删除动态</a></span></p>"; } $("#nr").html(str); //点击回复 } }); //当发表动态时,将动态内容写进数据库,并刷新页面 $(&quot;#fb&quot;).click(function(){ var dt= $(&quot;.xdt&quot;).val(); var uid = $(&quot;.qid&quot;).attr(&quot;yh&quot;); $.ajax({ url:&quot;main-cl.php&quot;, data:{dt:dt}, 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; ; } }); }) </script>
Copy after login

The above is the detailed content of PHP example - PHP imitates the function of posting updates, commenting on updates, replying to comments, deleting updates or comments in QQ space or Moments (Part 1). 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