2012-05-11 41 views
17

我目前正在研究一個移動應用程序,它可以讓您向朋友詢問最愛,它是一個HTML5前端和一個PHP後端。我堅持建立一個通知系統的最好方法是什麼,更重要的是數據庫模式比實際的代碼本身。在PHP中構建多通知系統的最佳方式

移動應用流程如下:

  • 用戶請求一個忙
  • 用戶可以選擇;通知所有的朋友,最喜歡的通知朋友或notifdy的closeby朋友
  • 根據用戶選擇了什麼,人們被告知

什麼是PHP和MySQL做到這一點的最好方法是什麼?我並不是真的要求任何人寫我的PHP代碼,而是映射出最理想的MySQL表和字段模式,而不是像我目前所堅持的那樣。在此先感謝您的幫助。

回答

29

您可以創建一個通知表,例如:

from_user | to_user | notification | seen 

,然後每當要通知用戶時,只需添加一條記錄與所需的信息,並設置seen0/false

然後當用戶讀取通知時,您將該參數設置爲1/true

例子:

from_user | to_user | notification | seen 
    -   -   -   - 

用戶John通知用戶Jeff:

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

用戶Jeff閱讀通知:

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

因此,基本上如果用戶選擇通知最喜歡的朋友,我首先會做一個查詢來獲取當前用戶收藏夾的用戶ID,然後對每個ID的通知表進行批量插入?這似乎夠合理。像這樣的東西原本在我腦海中浮現,但我想也許還有另一種方式。這似乎是合乎邏輯的事情。 –

+0

@道恩,是的。或者你可以做一個子查詢(http://dev.mysql.com/doc/refman/5.5/en/subqueries.html)。無論如何,這似乎是最符合邏輯和乾淨的方式。我還會刪除所有超過7-14天的通知來優化所有內容,但這取決於您。 – Shoe

+2

簡單但可靠的方法+1 –

4

爲什麼不只是列出了一個名爲表中的所有通知「通知」

id | user_id | from_user_id | notification 
  • ID =通知ID
  • USER_ID =誰是通知?
  • from_user_id =誰發送了通知?
  • 通知=消息

然後爲僞代碼:

// 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 
} 
+0

感謝您的配偶。我首先發布了傑弗裏的答案,但我很感激你花時間回覆,所以我投了票。 –

4

擴展傑弗裏答:

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

時間戳將幫助您確定在哪個時刻發生的特定通知。您還可以通過擴展腳本來創建時間片刻timeago.jsLivestamp.js

它還可以幫助您創建通知時間表,並且通知維護將更加簡單。