I have a database which when inserted into should create a notification in the webpage. What I did here was polling through ajax. I execute a query which selects for a row whose timestamp is less than 3 seconds from current time. Here is my code- HTML file:
<html>
<head>
<title>Dashboard</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3 /jquery.min.js"></script>
</head>
<body>
<div id="responsecontainer"></div>
<script type="text/javascript">
$( document ).ready(function() {
setInterval(ajaxcall, 500);
function ajaxcall() {
$.ajax({
type: "GET",
url: "maintdashsubmit.php",
dataType: "html", //expect html to be returned
success: function(response){
$("#responsecontainer").append(response);
}
});}
});
</script>
</body>
</html>
PHP file:
<?php
session_start();
$link = mysqli_connect("localhost", "root", "*****", "DMRC");
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM Ticket, Station, user_auth WHERE TIME(ticket_open_datetime) BETWEEN curtime()-3 AND curtime() AND Ticket.ticket_station_id = Station.station_id AND Ticket.ticket_open_emp_id = user_auth.emp_id AND Ticket.ticket_close_open='open" ;
$result = $link->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr><td style='width:15%''>".$row['ticket_id']."</td><td><table><tr><td>".$row['ticket_equipment']."</td></tr><tr><td>".$row['ticket_equipment_id']."</td></tr></table></td><td>".$row['station_name']."</td><td><table><tr><td>".$row['emp_firstname']." ".$row['emp_lastname']."</td></tr><tr><td> Sec:".$row['emp_section']."</td></tr><tr><td>".$row['emp_id']."</td></tr></table></td><td>".$row['ticket_close_open']."</td></tr>";
}
}
$link->close();
?>
This gives me the most recent notification that I want to see. But it displays multiple times as long as it remains within the 0.5 seconds time limit. I read that I can keep a column in the database that can be set 1 if the notification has been displayed and 0 if not.
But I have a doubt. Suppose this notification has to be received by many users. What if displaying a notification to one user sets the column entry 1 before it was viewed by another? This is all so confusing. If anyone can suggest a way?