我有三个表“ 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
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] 删除。
我来说两句