I'm making a live chat system for my site. Any help with solving this issue is greatly appreciated =). I have a page ticket.php with a div #reply-holder where I fetch the comments for that ticket using AJAX:
<script type="text/javascript">
function loadlink(){
$('#reply-holder').load('ajax.php',function () {
$(this).unwrap();
});
}
loadlink(); // This will run on page load
setInterval(function(){
loadlink() // this will run after every 5 seconds
}, 15000);
</script>
My ajax.php page where the comments are fetched from the MYSQL db and display is:
<?php
require_once 'inc/config.php';
session_start();
$ticketid = $_SESSION['ticketid'];
$username = $_SESSION['username'];
$getticketid = "SELECT * FROM orders where id = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());
$result = $link->query($getticketid);
$row = $result->fetch_array(MYSQLI_NUM);
$active = $row[2];
$created_at = $row[3];
$message = $row[4];
$price = $row[5];
?>
<?php
$getcomments = "SELECT * FROM comments where orderid = ".$ticketid." ORDER BY id DESC"
or die(mysql_error());
$result2 = $link->query($getcomments);
while($row2 = mysqli_fetch_array($result2)){
echo '<div class="reply">';
echo '<p>'.$row2[2].' replied:</p>';
echo '<h3>'.$row2[3].'</h3></div>';
$lastMessageIdWePlayedSoundFor = $row2[0] - 1;
}
?>
I've tried the following code, but the sound isn't working, no sound is played even if a new comment is inserted.. I first tried it on ajax.php but it would just play the sound after every refresh even if no new comment was inserted.
I'm pretty sure the code is alright, I just don't know exactly where to place it.
<?php
$playsound = "SELECT COUNT(*) FROM comments where orderid = '".$ticketid."' ORDER BY id DESC LIMIT 1"
or die(mysql_error());
$result2 = $link->query($playsound);
$row2 = $result2->fetch_array(MYSQLI_NUM);
$lastMessageId = $row2[0];
if($lastMessageId!=$lastMessageIdWePlayedSoundFor)
{
$myAudioFile = "notification.mp3";
echo '<EMBED SRC="'.$myAudioFile.'" HIDDEN="TRUE" AUTOSTART="TRUE"></EMBED>';
$lastMessageIdWePlayedSoundFor = $lastMessageId;
}
?>