I'm making an application using PHP, and I wanted to build a really simple page where a sound is played if two values in a MySQL row are set to '1' (true).
For instance, if none or only one of the values is set to 1, the script does not play the sound.
However, as soon as the second value changes to 1, there should be a JS 'listener' which detects this and automatically plays the sound.
Is there any way to achieve this?Answer1:
It can be done using webSockets by pushing a message to client side. here is a solution for <a href="http://socketo.me/" rel="nofollow">implementing webSockets in php</a>.
Also here is a good answer for <a href="https://stackoverflow.com/a/14515205/205997" rel="nofollow">How to create websockets server in PHP</a> if you want to do it yourself. But I can tell you that using php as webSocket server is not going to scale well if your user base is huge. You may want to consider doing it using <a href="http://nodejs.org/" rel="nofollow">Node.js</a>.
Another option: You can also check for update using ajax from time to time. but it will not be as instantaneous as webSocket solution. In this solution the server will receive 1 req per user each x seconds.Answer2:
PHP is a server side script and it's execute on the server while the JS is client side script. You can check for the values from the DB in php and if the two values are 1 && 1 you echo a
otherwise you echo empty function