在PHP中构建多通知系统的最佳方法

时间:2012-05-11 01:54:58

标签: php mysql codeigniter database-design database-schema

我目前正在开发一款移动应用,可让您向朋友询问收藏,它是HTML5前端和PHP后端。我不知道建立通知系统的最佳方式是什么,更多的是数据库模式而不是实际的代码本身。

移动应用程序流程如下:

  • 用户请求帮助
  • 用户可以选择;通知所有朋友,通知最喜欢的朋友或通知朋友
  • 根据用户的选择,通知人们

在PHP和MySQL中执行此操作的最佳方法是什么?我并不是真的要求任何人给我写PHP代码,而是映射出最理想的MySQL表和字段模式,因为这是我目前所坚持的。在此先感谢您的帮助。

3 个答案:

答案 0 :(得分:29)

您可以创建通知表,例如:

from_user | to_user | notification | seen

然后,只要您想要通知用户,只需添加包含所需信息的记录,并将seen设置为0 / false

然后,当用户阅读通知时,您将该参数设置为1 / true

示例:

from_user | to_user | notification | seen
    -          -          -           -

用户john通知用户jeff:

from_user | to_user | notification | seen
   john      jeff       whatever..    0

用户杰夫阅读通知:

from_user | to_user | notification | seen
   john      jeff       whatever..    1  

答案 1 :(得分:4)

为什么不在名为“notifications”的表中列出所有通知

id | user_id | from_user_id | notification
  • id =通知ID
  • user_id =谁是?
  • 的通知
  • from_user_id =谁发送了通知?
  • notification =消息

然后作为伪代码:

// Create a notification from User A to User B
$this->db->insert ('notifications', array ('user_id' => $friends_id, 'from_user_id' => $current_user_id, 'notification' => $message));

 // The meanwhile, on your home page or somewhere, wherever you want to display notifications
 $this->db->where ('user_id', $current_user_id)
 $notifications = $this->db->get ('user_id');
 foreach ($notifications as $notification)
 {
         // Display the notification as needed
         // Optionally delete the notification as it is displayed if it is a "one off" alert only
 }

答案 2 :(得分:4)

延伸杰弗里的答案:

id  |  from_user | to_user | notification | seen | timestamp
 -         -          -          -           -         -

时间戳将帮助您确定特定通知发生的时刻。您还可以通过使用timeago.jsLivestamp.js

扩展脚本来创建时间

它还可以帮助您创建通知时间表,并且维护通知会更简单。