简体   繁体   English

如何在php中显示评论通知

[英]How to show comments notification in php

I have three tables "posts", "comments" and "friend_request"我有三个表“posts”、“comments”和“friend_request”

screenshot截屏数据库结构

1: If someone comments on SESSION['id'], the notification must appear. 1:如果有人对 SESSION['id'] 发表评论,则必须出现通知。

Existing query for "posts"现有的“帖子”查询

SELECT * FROM posts WHERE post_user = '".$_SESSION['id']."'

post_id = postId;
post_user = postUser;

comments on posts对帖子的评论

SELECT * FROM comments WHERE post_id = '$post_id' AND user_id != '".$_SESSION['id']."' AND status = '0'"

I want if someone comments on my post, and I reply to the comments, I just want to show notifications on the commentator's sidebar only, even if there will more replies.我想如果有人评论我的帖子,我回复评论,我只想在评论员的侧边栏上显示通知,即使会有更多回复。

friend_request is a table where displaying either member are friends or not. friend_request 是一个表格,其中显示任一成员是否为朋友。

The way I would approach this is to use a client side AJAX request to a php fetch script that looks at the logged in users SESSION['id'] to find all comments made by post authors that are children of a comment the logged in user has made我处理这个问题的方法是使用客户端 AJAX 请求到 php fetch 脚本,该脚本查看登录用户SESSION['id']以查找帖子作者发表的所有评论,这些评论是登录用户的评论的子级造成

Frontend前端

You may use whatever way you want to send a request to your PHP script, I have used jQuery for simplicity.您可以使用任何方式向 PHP 脚本发送请求,为了简单起见,我使用了 jQuery。

The jQuery $.ajax function will be ran on the page continuously to check for notifications. jQuery $.ajax函数将在页面上持续运行以检查通知。 This has been done by enclosing it in a setInterval function.这是通过将其包含在setInterval函数中来完成的。

This code is not tested so not 100% sure on the namespacing of the PHP array you will get back.此代码未经测试,因此不能 100% 确定您将返回的 PHP 数组的命名空间。 Best to console.log the data object in the success handler here.最好在此处的成功处理程序中console.log data对象。

You will notice that there is reference to a readComment.php script, this can be a simple script that sets the comments.status = 1 to indicate read status.您会注意到这里引用了一个readComment.php脚本,这可以是一个简单的脚本,它设置comments.status = 1以指示读取状态。


  setInterval(function() {
    $.ajax({
   url:"fetch.php",
   method:"GET",
   success:function(data)
   {
    console.log(data)
    data.forEach(function(i,v) {
        $( "#notifications" ).append( "<a href=readComment.php?id=" + v.id + ">unread notification</p>" );
    });

   }
  });
 }
}, 60 * 1000); // This will run every minute to check for notifications

Server服务器

fetch.php

I am using a select statement here with two WHERE IN clauses to retrieve all comments made by post authors that are children of a comment the logged in user has made.我在这里使用带有两个WHERE IN子句的 select 语句来检索帖子作者所做的所有评论,这些评论是登录用户所做评论的子级。 I am not 100% on this query either as I have not tested but this should get you started.我也不是 100% 这个查询,因为我还没有测试过,但这应该让你开始。

$query = "SELECT 
    *
FROM
    comments
WHERE
    comments.user_id IN (SELECT posts.post_user FROM posts)
AND
    comments.parent_comment_id IN (SELECT comments.id FROM comments WHERE comments.user_id = ".SESSION['id'].")"
AND 
    comments.status = 0";

$result = mysqli_query($connection, $query);

header("Status: 200");
header('Content-Type: application/json');
echo json_encode(array('data' => mysqli_fetch_array($result) )); 
die;

Let me know if you need more information.如果您需要更多信息,请与我们联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM