2012-04-11 198 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的用戶(用戶ID爲2和1)也在帖子中留言?

如果我應該使用第二張表,請告訴我結構應該如何。由於

+0

你需要一些東西來確認帖子不是嗎?您上面粘貼的查詢僅通過user_id從數據庫中進行選擇。 – Pete 2012-04-11 10:46:21

+0

你怎麼知道通知被讀取,並且不再顯示它? – safarov 2012-04-11 10:47:37

+0

@Pete,該帖子已經有一個唯一的ID。我發佈的查詢現在不是真正的挑戰,但是查詢將通知發送給評論同一帖子的其他用戶。 – Chibuzo 2012-04-11 10:49:45

回答

1

我建議分離的文章和評論到單獨的表中有一個一對多的關係

後{ ID, USER_ID, 內容, 日期 }

評論{ ID, POST_ID , user_id, comment, date }

然後,您可以通過超時ajax調用來輪詢您的評論。如果註釋是由不是您的user_id添加的,請在頁面中注入通知。

+0

每篇文章和評論都有一個表格,正如你所建議的那樣。我希望對帖子發表評論的其他用戶也收到通知,不僅是帖子所有者。 – Chibuzo 2012-04-11 11:15:56

+0

當然,無論何時用戶在帖子頁面上,您都會針對該帖子提交的任何評論進行投票。如果有的話,注入一個通知。 – 2012-04-11 11:24:58

+0

我明白了你的觀點,但情況是,我希望用戶能夠在他們的用戶頁面上獲得通知,然後再鏈接到帖子頁面。不管怎麼說,還是要謝謝你。 – Chibuzo 2012-04-11 11:33:07

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。

相關問題