想要从 Mysql 中检索记录数据并将其存储到 Javascript 数组中,以获取 heatmap.js 地图数据,格式如下:
var testData = {max: 46, data: [{lat: 33.5363, lon:-117.044, value: 1},{lat: 33.5608, lon:-117.24, value: 1},..]};
现在我被困在这里,我不知道如何从 Jquery 连接到我的var testData = new Array();
,我应该怎么做才能解决这个问题?
(更新的正确代码)
get_query.php
<?php
require_once('./db_con.php');
$dbcon=new db;
$query="SELECT (SELECT geo_lat FROM fun WHERE created_at <= DATE_SUB(NOW(), interval 1 minute) AS geo_lat," .
"(SELECT geo_long FROM fun WHERE created_at <= DATE_SUB(NOW(), interval 1 minute) AS geo_long";
$result = mysqli_query($dbcon,$query);
$data = array();
while($row= mysqli_fetch_assoc($result)){
$data[] = array("lat"=>$row["geo_lat"], "lon"=>$row["geo_long"], "value"=>1);
$post_data = json_encode(array('max' => 46, 'data' => $data));
}
echo $post_data;
?>
my_data.js 基于此处:
jQuery.extend({
getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
result = JSON.stringify(data);
}
});
return result;
}
});
var testData = $.getValues("get_query.php");
感谢 Orangepill 和 Chrislondon。