如何在PHP中显示评论通知

垃圾书

我有三个表“ posts”,“ comments”和“ friend_request”

屏幕截图 数据库结构

1:如果有人对SESSION ['id']发表评论,则该通知必须出现。

现有查询的“职位”

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

post_id = postId;
post_user = postUser;

帖子评论

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

我希望有人在我的帖子上发表评论,然后回复这些评论,我只想仅在评论者的侧边栏显示通知,即使会有更多回复。

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

Frontend

You may use whatever way you want to send a request to your PHP script, I have used jQuery for simplicity.

The jQuery $.ajax function will be ran on the page continuously to check for notifications. This has been done by enclosing it in a setInterval function.

This code is not tested so not 100% sure on the namespacing of the PHP array you will get back. Best to console.log the data object in the success handler here.

您会注意到,这里有对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

服务器

fetch.php

我在这里使用带有两个WHERE IN子句的select语句来检索帖子作者发表的所有评论,这些评论是已登录用户发表的评论的子级。我没有对此测试进行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;

如果您需要更多信息,请与我们联系。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在SUM()中添加SELECT查询以显示论坛中的评论总数?

如何在Android 10中显示每日离线通知?

如何在Android Studio中显示通知图标

如何在Android中显示多个通知

如何通知评论者?

如何突出显示评论中的类型?

如何在Angular 2中显示通知

如何在通知中心显示推送通知

如何在Oreo中设置徽章计数而不显示通知?

如何在Laravel中显示多个吐司通知

默认情况下,如何在Firefox的HTML元素属性中显示PHP通知?

如何在具有最新评论的帖子中“突出显示”帖子,并在Rails的顶部显示没有评论的新帖子?

如何使用MySQL在php中显示每个餐厅的评论?

如何在布局中显示Firebase通知消息

如何在Rails中向所有评论者发送有关多态评论的通知?

如何在Laravel中显示通知?

如何在状态栏中显示默认的静默通知?

如何在Android中显示特定数量的通知

如何在Windows气球通知中显示图像?

如何在django中显示评论数

我如何在单个论坛帖子中显示很多评论

如何在while循环中对php中的内容发表评论

如何在wordpress帖子中仅显示评论作者(未注册)的未批准评论?

如果多行已经包含评论,如何在 sublime 中评论?

如何在查询结果中显示评论字段?

如何在 rails 中显示帖子的评论(通过迭代)

如何在 Laravel 中显示评论和回复评论

我如何在最后发表的评论上方显示网站评论

如何在postgres中成功显示通知