【问题标题】:How to implement comment notification?如何实现评论通知?
【发布时间】:2012-04-23 15:56:48
【问题描述】:

我正在尝试在 cmets 上实现通知,也就是说 - 如果我对一个帖子发表评论,而另一个用户 cmets 在同一个帖子上发表评论,我会收到通知。我已使用此查询为帖子所有者处理了通知

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

在上表中,owner_id 是帖子的所有者,user_id2 是评论帖子的人。

我如何告诉 id 为 17 的用户有两个人(用户 id 为 2 和 1)也评论了帖子?

如果我应该使用第二张桌子,请告诉我结构应该如何。谢谢

【问题讨论】:

  • 您需要一些东西来识别帖子,不是吗?您在上面粘贴的查询仅按 user_id 从数据库中选择。
  • 你怎么知道通知已被阅读,并且不再显示?
  • @Pete,帖子已经有了唯一的 ID。我发布的查询现在并不是真正的挑战,而是向评论同一帖子的其他用户发送通知的查询。
  • @safarov,我已经想到了,但我想先收到通知。但是,每个显示的通知都有一个删除通知链接,因此所有者可以根据需要删除通知。
  • @Chibuzo 删除通知不正确。例如,如果用户 17 阅读了通知,但用户 2 没有。并且永远不会通知用户 2

标签: php mysql


【解决方案1】:

以下查询获取评论过的用户

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

但您不想通知当前发帖的人,所以...

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

还是我错过了什么?

【讨论】:

    【解决方案2】:

    我建议将帖子和 cmets 分成具有一对多关系的单独表

    发布{ ID, 用户身份, 内容, 日期 }

    评论{ ID, post_id, 用户身份, 评论, 日期 }

    然后您可以通过超时 ajax 调用简单地轮询您的 cmets。如果 cmets 已由不是您的 user_id 添加,请在页面中注入通知。

    【讨论】:

    • 按照您的建议,post 和 cmets 各有一个表。我希望帖子上的其他用户也收到通知,而不仅仅是帖子所有者。
    • 当然,只要有任何用户在帖子页面上,您就会轮询为该帖子提交的任何 cmets。如果有,请注入通知。
    • 我明白你的意思,但情况是我希望用户在他们的用户页面上获得通知,然后获得指向帖子页面的链接。还是谢谢。
    • 因此,您在用户创建(或评论)的任何帖子上轮询所有 cmets。
    【解决方案3】:

    我创建的这个课程将为您提供详细信息。我已经用“notify_chain”方法完成了你需要做的事情。您将需要查看其他功能,因此我将在此处为您发布整个课程。

    <?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();
    
    ?>
    

    我希望这会有所帮助。如果您有任何问题,请查看 facebook 上的 maestrojosiah PG。

    【讨论】:

      猜你喜欢
      • 2015-12-19
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 2015-08-26
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多