How to play a sound when a MySQL value is changed

Viewed 766

I am developing a Queue System Managment with PHP and MySQL. To summarize, I have a database which saves the 4 kinds of ticket that you can get. When the people who work here want to call the next person, they have a dashboard with a button that adds +1 to the MySql table current value. Then, I have a query to read data from the database which is updated every second to the index.php. It seems a little confusing but it works haha. The thing is - we have been having an issue, which is, people never know when its their time. We would like to implement a sound, so when someone is called, a sound is played

Index.php:

<div id="show"></div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        setInterval(function () {
            $('#show').load('data.php')
        }, 1000);
    });
</script>

Data.php

$result = $conn->query("SELECT alunos FROM senhas");
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo '<div class="alunos">';
        echo $row['alunos'] . '<br>';
        echo '</div>';
    }
}
$result = $conn->query("SELECT sase FROM senhas");
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo '<div class="sase">';
        echo $row['sase'] . '<br>';
        echo '</div>';
    }
}
$result = $conn->query("SELECT matriculas FROM senhas");
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo '<div class="matriculas">';
        echo $row['matriculas'] . '<br>';
        echo '</div>';
    }
}
$result = $conn->query("SELECT outrosassuntos FROM senhas");
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo '<div class="outros">';
        echo $row['outrosassuntos'] . '<br>';
        echo '</div>';
    }
}

matriculasavancar.php (it makes the category "Matriculas" avance 1 value)

$sql = "UPDATE senhas set matriculas = matriculas+1";
mysqli_query($conn, $sql);
header("refresh:0; url=admin.php");
2 Answers

I'm not 100% clear, but I think you want a sound whenever matriculas increments, and when matriculas increments the next time data.php is loaded there will be one more <div class='matriculas'> added to the page. It also seems this question is about how to track the value being changed versus finding out how to literally play the sound.

If I got that right, you could just keep a running count of .matriculas and do something whenever it increases.

$(document).ready(function() {
  function playSound() {
    // write your sound playing function
  }

  let matriculasOldCount = 0,
      matriculasNewCount = 0;

  setInterval(function () {
     $('#show').load('data.php');
     matriculasNewCount = $('.matriculas').length;
     if (matriculasNewCount > matriculasOldCount) {
       playSound();
     }
     matriculasOldCount = matriculasNewCount;
  }, 1000);
});

I think that you can use document.create and setAtribute to help you. Then from there, you can create an <audio> element and make it play the noise. However in PHP, this might not work. But in your js you can make a variable linking to the sound.

Related