我是AJAX的新手,我不知道如何获取阵列,我希望通过AJAX实时自动更新通知栏。如果我只是从气球更新数字,但我已经完成了工作,但我也无法通过PHP / AJAX从MySQL获取消息
$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);
}
}
这是AJAX,如果我只得到计数,但是我不知道如何使用AJAX这个PHP循环以及如何获得两个输出?
<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>
这就是我想要做的。 红色是警报的数量,但我不知道如何使两者都工作 IMAGE HOW LOOK IN WEB
答案 0 :(得分:0)
出于安全考虑,请使用mysqli。
你的问题在于你需要只放置1个json响应,这里至少提供2个。然后,只需1个查询和一个简单的循环就可以简化所有这些。像(未经测试)的东西:
$objMysql = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$r = array();
$comment_count = 0;
if(!$objMysql->connect_error){
$strQuery = "select a.num as comment_count, b.* from
(select count(*) as num from {$tableName} where is_read=0) as a,
(select * from notification order by execute_at desc) as b";
$res = $objMysql->query($strQuery);
while($row = mysql_fetch_assoc($res)){
$comment_count = $row['comment_count'];
$r[] = array(
'data' => date("Y-m-d H:i:s", strtotime($rowsmall["execute_at"])),
'command' => $row['command'],
'server' => $row['hostname'],
'status' => $row['status'],
'color' => ($row['is_read'] == 0)? 'red' : 'blue'
);
}
}else{
$r[] = 'mysql connect error: '.$objMysql->connect_error;
}
echo json_encode(array('comment_count' => $comment_count, 'results' => $r));
之后,您的所有值都将出现在javascript / jquery的response
变量中。