2012-04-11 14 views
2

私はコメントに通知を実装しようとしています。つまり、私が投稿にコメントし、同じ投稿に他のユーザーのコメントがあれば、通知を受け取ります。私はこのクエリ上記の表でコメント通知の実装方法

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 

notification table

を使用して、ポストの所有者のための通知を処理してきた、owner_idはポストを所有している人で、user_id2はポストにコメントしているものです。

ID 17のユーザーに2人のユーザー(ユーザーID 2と1)も投稿にコメントしたとどのように伝えますか?

2番目のテーブルを使用する必要がある場合は、その構造がどうあるべきか教えてください。おかげ

+0

あなたはポストをIDにする何かが必要ですか?上に貼り付けたクエリは、user_idによってデータベースからのみ選択します。 – Pete

+0

通知が読み取られたことをどのように知っていますか? – safarov

+0

@ Pete、その投稿は既に一意のIDを持っています。私が掲示した質問は今は本当に挑戦ではなく、同じ投稿にコメントした他のユーザーに通知を送るためのクエリです。 – Chibuzo

答えて

1

は、私は多くの関係に1

ポスト{ ID、 のuser_id、 コンテンツ、 日付 }

コメント{ ID、 post_idのと別のテーブルに分離投稿やコメントを示唆します、 user_id、 コメント、 日時 }

次に、タイムアウトajax呼び出しでコメントをポーリングできます。あなたのものではないuser_idによってコメントが追加されている場合は、ページに通知を挿入します。

+0

投稿とコメントにはそれぞれ、提案したとおりの表があります。投稿のコメントだけでなく、通知も受け取るように、他のユーザーに通知したい。 – Chibuzo

+0

確かに、ユーザーが投稿ページにいるときはいつでも、その投稿に対して投稿されたコメントを投票します。そしてもしあれば、通知を注入してください。 –

+0

私はあなたの要点を得ていますが、ユーザーのユーザーページで通知を取得してから、ポストページへのリンクを取得したいという状況があります。とにかくありがとう。 – Chibuzo

2

次のクエリは、

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' 

それとも私が行方不明です何か?

0

私が作成したこのクラスはあなたに詳細を与えます。私はあなたが '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をチェックしてください。

関連する問題