2016-01-22 27 views
0

我在嘗試製作通知系統,通知用戶將其分配給故障單或將新故障單添加到數據庫時。通過ajax在網站上提醒用戶

我已經擁有的系統本身的工作原理除了它只將通知發送給接收ajax請求的第一個用戶。有什麼辦法可以讓每個想要收到通知的人都能收到通知?

我的代碼: 的javascript:

 function checkUpdates() 
      { 
       $.ajax({ 
        type: "POST", 
        url: 'ajax/checkDB.php', // a webservice or other URL that queries the database 
        data: {}, 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function(data) { 
         console.log('Connected and executed PHP page... '+data); 
         if (data.hasChanged == "true") { 
          playSound('img/notif2'); 
          notifyAdmin(); 
          console.log('Updated Tickets Page...'); 
          $("#contents").load("dynamic/tickets.php"); 
          $("#contents").fadeTo("fast", 1); 
         } 

         if (data.newAssigned == "true") { 
          playSound('img/notif2'); 
          notifyUser(); 
          console.log('Updated Tickets Page...'); 
          $("#contents").load("dynamic/tickets.php"); 
          $("#contents").fadeTo("fast", 1); 
         } 
        } 
       }); 
      } 
     $(document).ready(function() { 
      setInterval("checkUpdates()", 3000); // Calls the function every 3 seconds 
     }); 

我的PHP腳本(checkDB.php):

<?php 
include("../static/config.php"); 
session_start(); 

header("Content-Type: text/json"); 

     $result = mysqli_query($con,"SELECT notify FROM tickets WHERE notify='0'"); 
     $row_cnt = mysqli_num_rows($result); 

     if($row_cnt > 0) { 
      $hasChanged = 'true'; 
      mysqli_query($con,"UPDATE tickets SET notify='1' WHERE notify='0'"); 
     } else { 
      $hasChanged = 'false'; 
     } 

     $result2 = mysqli_query($con,"SELECT notify,Assigned FROM tickets WHERE Completeness='1' AND notify='1'"); 
     $row_cnt2 = mysqli_num_rows($result2); 

     if($row_cnt2 > 0) { 
      while($row2 = mysqli_fetch_array($result2)) 
       { 
        if(strcmp($_SESSION['Name'],$row2['Assigned']) == 0) { 
         $newAssigned = 'true'; 
        } else { 
         $newAssigned = 'false'; 
        } 
       } 
      mysqli_query($con,"UPDATE tickets SET notify='2' WHERE Completeness='1' AND notify='1'"); 
     } else { 
      $newAssigned = 'false'; 
     } 

     echo json_encode(array('newAssigned' => $newAssigned, 'hasChanged' => $hasChanged)); 

?> 

這裏是我的確切意圖的破敗: 用戶登錄到系統中,並給予行政權利。 用戶加載票據頁面,其中列出了所有票據以及我給你的這個javascript JavaScript每3秒加載一次checkDB.php,基本上運行一個針對數據庫的'notify'值爲0的檢查,並且如果在至少存在1個,在數據庫中更新爲'1',並返回爲正確的ajax調用 - 發送通知。

再次,這適用於加載到頁面的第一個用戶,但在此之後,顯然在數據庫中不再有'notify = 0',因爲它們已經被更新,所以它們不會顯示通知。

有人要求我分享我的數據庫結構。 門票:

UniqueID - Unique ID per ticket (always unique) 
Requester - Whoever submitted the ticket. 
Problem - Description of the given issue. 
Assigned - User who is assigned to the ticket. 
Completeness - The level of completeness (0-4) 
Times - Times listed for ticket start, assigned, etc 
notified - Used in old ticket system (I plan to get rid of it) 
Urgency - Selected by requester, how urgent the ticket is 
Location - Location in our warehouse assistance is needed. 
FollowUp - currently not in use, will be used to submit follow ups. 
isProject - (0-1) is project or not 
additionalTechs - Lists additional users on a ticket (Not important for question) 
notify - (0-2) at 0, ticket is new and notif should be sent to admins which should set this to 1, when it is 1 it should send a notif to users that are attached to the ticket. 

科技股數據庫:

UniqueID 
Username 
Password 
Name 
Level 
Disabled 
LastTimeSeen 
needsNotify (Used in old system, plan to remove) 

我真的被困在這裏。

+0

請告訴我買票的表結構?請列出欄目 – Kisaragi

+0

兩列....用戶和通知。如果用戶爲空,則它尚未分配。查詢應該是基於用戶的 – charlietfl

+0

看起來您需要將您的SQL查詢限制爲當前用戶 – medinasod

回答

0

你可以使用websockets或socket.io。 那麼多個客戶端的JS將與插座連接會通知所有連接的人們瞭解他們票

+1

嗨欒。更好地使用評論框來提出建議。 –

+0

你能深入解釋這些東西是如何工作的嗎?我以前從未使用過它們。 – GrumpyCrouton

+0

Websocket就像一個隧道,瀏覽器與服務器1進行通信,並保持打開狀態直至關閉它。順便說一下,這非常安全。 (還有其他應用程序) 它被廣泛用於創建人們連接的聊天室,並且當服務器意識到接收到新消息時,將其發送給所有與其連接的人。 一個漂亮的插件是http:// socket。io/ –

0

你可以改變你的更新語句是這樣的:

UPDATE tickets SET notify='1' WHERE notify='0' and Assigned = ''//some user id passed 
+0

我不認爲這會起作用,因爲管理員不是, t分配給票證。我目前正在嘗試確保通知已發送給所有管理員,這些管理員在登錄時設置在會話變量中。 – GrumpyCrouton

+0

我想我不明白你想要做什麼/問。 – Kisaragi

+0

當提交故障單時,應向網站上的所有管理員發送通知 – GrumpyCrouton