简体   繁体   中英

Add a sound when receiving new order

i need help with adding a sound when i receive a new order, i have an autorefresh at 1min and also i have an embedded sound. but i want to play that sound only if a new order arrived not at every refresh, and i dont have any idea right now how to make it.

$query="select * from Order where Stare=0 order by ID_Order desc";
    $result = mysql_query($query)
            or die("query failed: " . mysql_error());   
        while ($row = mysql_fetch_array($result))
        {
            $id_order=$row['ID_Order'];
            $id_oferta=$row['ID_Oferta'];
            $nume=$row['Nume'];
            $prenume=$row['Prenume'];
            $nume_tot=' '.$nume.' '.$prenume.'';
            echo'<tr><td>'.$id_order.'</td>
                     <td>'.$id_oferta.'</td>
                     <td>'.$nume_tot.'</td>
                </tr>';

                 echo'   <embed src="sounds/button-9.mp3" autostart="true" hidden="true" loop="true" name="jukebox"> ';

        }

You need to save the state of you last query and compare it with the current one.

You could use sessions for that:

// at the top of your script
sesstion_start();

  // in your loop
  $last_order = ID_of_the_first_row_found;    // only save the first row ID, use a boolean or counter to keep track of that

// after your loop
if (!isset($_SESSION['last_order']) || $last_order !== $_SESSION['last_order'])
{
  // add the sound
}
$_SESSION['last_order'] = $last_order;

Ideally you would do the refresh in javascript using ajax so that you don't have to refresh the whole page and get a nicer user experience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM