我在嘗試製作通知系統,通知用戶將其分配給故障單或將新故障單添加到數據庫時。通過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)
我真的被困在這裏。
請告訴我買票的表結構?請列出欄目 – Kisaragi
兩列....用戶和通知。如果用戶爲空,則它尚未分配。查詢應該是基於用戶的 – charlietfl
看起來您需要將您的SQL查詢限制爲當前用戶 – medinasod