You need javascript ajax and json and callback function to check constantly. It can be done by push services but PHP is bad with that and you need websockest, etc. facebook uses timeout calls. not sure for the stackoverflow
refreshInterval = 500
refreshTimeout = setTimeout( getNotificationCounts, refreshInterval );
function getNotifications() {
$.ajax({
url : 'path_to_your_php',
type : 'POST',
data: //put for exampel user_id but it cvan be handled by sesion as well
dataType : 'json',
success : function(data) {
alert('You have'+ data.total +' new messages')
refreshTimeout = setTimeout( getNotificationCounts, refreshInterval ); //this will check every half of second
}
});
}
then in PHP, for example you have a flag in database with which you check if data is new to teh databse. Mind the formatting and to proper sql escape
$result=mysql_query("SELECT count(*) as total from myTable where user_id=$_POST['user_id'] AND is_read=0");
//note instad of $_POST you can use session if users are logged in, or however you are handling it
$data=mysql_fetch_assoc($result);
echo json_encode($data)
;
After that you can create another ajax call that marks messages as read once user clicks on them. or opens a dialog listing or wahtever
mysql_query("UPDATE myTable WHERE user_id=$_POST['user_id'] SET is_read=1");