Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  15] [ 2]  / answers: 1 / hits: 5756  / 11 Years ago, mon, february 3, 2014, 12:00:00

I have a desktop application inserting values into a mysql table with fields id, keyword and timestamp



I have a php page which is kept open in my browser and lists all entries from that database.



What I want to do is to have this page automatically check if there's a new entry in the database and warn me. This warning must be a sound.



I consider an entry to be new if it's timestamp is less than 60 seconds the current timestamp.



So, I have the mysql query which does check for new entries, but my main question is, how to perform that query every 1 second without manually refreshing the page? And how to play a sound alarm when a new entry is found?



Thanks in advance.


More From » php

 Answers
10

This is how I managed to do it:





Within the page's <head> node, the following code:



<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script>

<script type=text/javascript>// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#divToRefresh').load('dbcheck.php');
}, 500); // refers to the time to refresh the div. it is in milliseconds.
});
// ]]></script>




Then, somewhere in the page:



<div id=divToRefresh>Running...</div>




And naturally, I've created a new php file called dbcheck.php containing which connects to the database and contains the following:



$sql3 = SELECT keyword, timestamp FROM keywords ORDER BY id DESC LIMIT 1;;
$query3 = $pdo->prepare($sql3);
$query3->execute();
$result3 = $query3->fetch();

$timestampCheck = time() - 120;
if ($result3['timestamp'] >= $timestampCheck)
{
echo <div>NEW KEYWORD FOUND!</div>nn;
echo <div>, $result3['keyword'], </div>;

echo <audio autoplay=autoplay>
<source src=alarm.wav type=audio/wav>
Your browser does not support the audio element.
</audio>;

}
else
{
echo Listening...;
}

[#48112] Sunday, February 2, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sageallenv

Total Points: 458
Total Questions: 102
Total Answers: 104

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;