简体   繁体   English

如何实施评论通知?

[英]How to implement comment notification?

I am trying to implement notifications on comments, that is - if I comment on a post and another user comments on the same post, I'll get a notification. 我正在尝试对评论实施通知,即-如果我对某条信息发表评论,而另一位用户对同一条评论发表评论,我将收到一条通知。 I've handled the notification for the post owner using this query 我已经使用此查询为帖子所有者处理了通知

SELECT id, owner_id, post_id, user_id2, COUNT(user_id2) AS num, type, UNIX_TIMESTAMP(date_done) AS date 
FROM notification
WHERE owner_id = '$user_id' AND user_id2 != '$user_id' 
ORDER BY date_done DESC

通知表

In the table above, owner_id is the person that owns the post, the user_id2 are those that commented on the post. 在上表中, owner_id是拥有该职位的人,在user_id2是那些在岗位评价。

How do I tell the user with id 17 that two persons (with user id 2 and 1) also commented on the post? 我如何告诉ID为17的用户两个人(用户ID为2和1)也对帖子发表了评论?

If I should use a second table, please tell me how the structure should be. 如果我要使用第二张表,请告诉我结构应该如何。 Thanks 谢谢

The following query gets the user(s) who have commented 以下查询获取发表评论的用户

SELECT   DISTINCT user_id2 
FROM notification
WHERE post_id = '$post_id'

But you don't want to notify the person currently posting so... 但是您不想通知当前发帖的人,所以...

SELECT   DISTINCT user_id2 
FROM notification
WHERE post_id = '$post_id'
AND user_id2 != '$user_id'

Or am I missing something? 还是我错过了什么?

I suggest separating posts and comments into separate tables with a one to many relation 我建议将帖子和评论分成一对多的关系并放在单独的表中

post { id, user_id, content, date } 发布{id,user_id,内容,日期}

comment { id, post_id, user_id, comment, date } 评论{id,post_id,user_id,评论,日期}

Then you can simply poll your comments via a timeout ajax call. 然后,您可以通过超时ajax调用简单地轮询您的评论。 If comments have been added by a user_id that's not your, inject a notification into the page. 如果注释不是由您的user_id添加的,请在页面中插入通知。

This class that I created will give you the detail. 我创建的此类将为您提供详细信息。 I have done what you need to do with the method 'notify_chain'. 我已经完成了使用方法“ notify_chain”需要做的事情。 You will need to see the other functions, so I will post the whole class for you here. 您将需要查看其他功能,因此我将在这里为您发布整个课程。

<?php 


class Notification extends DatabaseObject{

public $id;
public $type;
public $post_id;
public $owner_id;
public $user_id2;
public $date_done;
public $read_or_not;
public $notifications;
public $message;
public $count;

protected static $table_name="notifier";
protected static $db_fields = array('id', 'type', 'post_id', 'owner_id', 'user_id2', 'date_done', 'read_or_not');


public function check_notification(){
    global $database;
    global $session;
    if($session->is_logged_in()) { 
    $id=$session->user_id;

    $sql = "SELECT * FROM notifier WHERE owner_id={$id} AND read_or_not = 0";
    $chk = $database->query($sql);
    if(!empty($chk)){
        while($notifs = $database->fetch_assoc($chk)){
            $message="";
        $this->notifications[]  = $notifs;
        $this->id                           = $notifs['id'];
        $this->type                         = $notifs['type'];
        $this->post_id                  = $notifs['post_id'];
        $this->owner_id                 = $notifs['owner_id'];
        $this->user_id2                 = $notifs['user_id2'];
        $this->date_done                = $notifs['date_done'];
        $this->read_or_not          = $notifs['read_or_not'];
            foreach($notifs as $notif){
                $user=Member::get_name($this->user_id2, false);
                if($this->type=="comment"){
                    $message = "<a href='post.php?post=".$this->post_id."&ntf_clr=".$this->post_id."'>".$user." commented on your post.</a>";
                } elseif($this->type=="reply"){
                    $message = "<a href='comment_reply.php?c_id=".$this->post_id."&ntf_clr=".$this->post_id."&cmt=Comment&rpl=CommentReply'>".$user." replied to your comment.</a>";
                } elseif($this->type=="discussion_comment"){
                    $message = "<a href='discussion.php?discussion=".$this->post_id."&ntf_clr=".$this->post_id."&cmt=Comment&rpl=CommentReply'>".$user." Commented on your discussion.</a>";
                } elseif($this->type=="discussion_reply"){
                    $message = "<a href='comment_reply.php?c_id=".$this->post_id."&ntf_clr=".$this->post_id."&cmt=Comment&rpl=CommentReply'>".$user." replied to your discussion comment.</a>";
                } elseif($this->type=="article_comment"){
                    $message = "<a href='article.php?article=".$this->post_id."&ntf_clr=".$this->post_id."'>".$user." Commented on your article.</a>";
                } elseif($this->type=="article_reply"){
                    $message = "<a href='comment_reply.php?c_id=".$this->post_id."&ntf_clr=".$this->post_id."&cmt=ArticleComment&rpl=ArticleCommentReply'>".$user." replied to your article comment.</a>";
                } elseif($this->type=="chain_comment"){
                    $message = "<a href='post.php?post=".$this->post_id."&ntf_clr=".$this->post_id."'>".$user." also commented to a post.</a>";
                } elseif($this->type=="chain_reply"){
                    $message = "<a href='comment_reply.php?c_id=".$this->post_id."&ntf_clr=".$this->post_id."&cmt=Comment&rpl=CommentReply'>".$user." also replied to a comment.</a>";
                } 
            }
            $this->message = $message_array[] = $message;
        } if(isset($message_array)){return $message_array;}         
        } else{
            return false;
        }
        }   
        }

        public static function mark_as_seen($notif_id){
            global $database;
            global $session;
            $me = $session->user_id;
            $sql = "UPDATE `notifier` SET read_or_not=1 WHERE owner_id = {$me} AND post_id={$notif_id}";
            $clear_notification = $database->query($sql);
        }

        public static function make_notification($post_id, $owner_id, $user_id2, $type="comment"){
            $notif = new Notification();
            $notif->type=$type;
            $notif->post_id=$post_id;
            $notif->owner_id=$owner_id;
            $notif->user_id2=$user_id2;
            $notif->date_done=strftime("%Y-%m-%d %H:%M:%S", time());
            $notif->read_or_not=0;
                $notif->create();
        }


        public static function notif_count($id){
        global $database;
        $notif_cnt = $database->query("SELECT COUNT(*) as notifs FROM notifier WHERE owner_id={$id} AND read_or_not = 0");
        $data=$database->fetch_array($notif_cnt);
        return $data['notifs'];
        }        

        public static function notify_chain($post_id, $user_id2, $type="chain_comment"){
        global $database;
        global $session;
            $sql = "SELECT DISTINCT user_id2 ";
            $sql .= "FROM notifier ";
            $sql .= "WHERE post_id = {$post_id} ";
            $sql .= "AND user_id2 != {$session->user_id} ";
            $chain = $database->query($sql);
        while ($users = $database->fetch_assoc($chain)){
            $list = "";
            foreach ($users as $user){
                Notification::make_notification($post_id, $user, $user_id2, $type="chain_comment");
                $list = $user;
            }
            $list_array[] = $list;
        } return $list_array;

        }

}

$notif = new Notification();

?>

I hope that helps. 希望对您有所帮助。 check out maestrojosiah PG at facebook if you have any problems. 如果有任何问题,请在facebook上查看maestrojosiah PG。

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

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