想從MySQL檢索記錄數據,並將其存儲到Javascript數組用於heatmap.js地圖數據格式爲:獲得MySQL數據,並將其存儲到Javascript數組
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();
,我該怎麼做才能解決這個問題?
(修訂版CORRECT CODE)
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從here基於:
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。
whoot' while(true){'? –
爲什麼你有'while(true){'? –
一旦數組按照你想要的方式構建,'json_encode'應該只調用一次。 –