2016-01-22 57 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