I'm new in AJAX and I dont know how to get arrays, I'm looking to automatically update an notification bar in real time with AJAX. I have done the work if I'm just updating the number from ballon but I can't get also the messages from the MySQL with PHP/AJAX
$sql=mysql_query("select * from $tableName where is_read=0");
$comment_count=mysql_num_rows($sql);
$comm_array = array('count' => $comment_count);
//first output need for get number of notications
echo json_encode($comm_array);
$listsql=mysql_query("select * from notification order by execute_at desc");
while($rowsmall=mysql_fetch_array($listsql))
{
$n_id=$rowsmall['notification_id'];
$date=date("Y-m-d H:i:s", strtotime($rowsmall["execute_at"]));
$command=$rowsmall['command'];
$server=$rowsmall['hostname'];
$status=$rowsmall['status'];
$variable[] = array( 'data' => "$date",
'command' => "$command",
'server' => "$server",
'status' => "$status" );
if($rowsmall['is_read'] == 0)
{
//this was just a try for get with different colors the unread notification
//second output - get notification from mysql
echo json_encode($variable);
}
else
{
//this was just a try for get with different colors the read notification
//second output - get notification from mysql
echo json_encode($variable);
}
}
This is the AJAX and work if I get only the count but I don't know how to get with AJAX this PHP while loop and how to get both outputs ?
<script>
$(document).ready(function() {
setInterval("ajaxcall()",2000);
});
function ajaxcall() {
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
type: "GET",
url: '/notifications/api.php', //the script to call to get data
success: function(response){ //on recieve of reply
json_object = JSON.parse(response)
var count = json_object.count;
var date = json_object.notifications.data;
var server = json_object.notifications.server;
var command = json_object.notifications.command;
var status = json_object.notifications.status;
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
$('#mes').html(count); //Set output element html
$('#not_read').html("Date: "+date+" Host: "+server+" Command: "+command+" Status: "+status);
//recommend reading up on jquery selectors they are awesome
// http://api.jquery.com/category/selectors/
}
});
}
</script>
This is what i want to do. with red is the number of alerts which works but i dont know how to make both work IMAGE HOW LOOK IN WEB