我不知道如何让PHP与我的JSON JavaScript代码一起使用。我假设我需要它将每一行解析为一个主数组,该数组为每一行分配一个从 1 到无穷大的唯一值键。但是,老实说,我已经看过了,我不知道该怎么做。另外,如果您发现映射添加标记有任何问题,请告诉我。
PHP代码:
<?php
include 'dbconnect.php';
$result = mysql_query("SELECT * FROM coords ORDER BY name DESC") or die ("Could
not query");
while($row = mysql_fetch_array($result)) {
$r[] = array(
"name" => $row['name'],
"lat" => $row['lat'],
"lng" => $row['lng'],
"speed" => $row['speed'],
"altitude" => $row['altitude'],
"distance" => $row['distance']
);
}
$encoded = json_encode($r);
echo $encoded;
exit($encoded);
mysql_close($conn);
?>
JAVASCRIPT 代码:
var usermarker;
var markloc;
function deleteUserOverlay() {
if (usermarker) {
usermarker.setMap(null);
}
}
function calluserlocation(){
console.log('calluserlocation fires');
$.ajax( {
url: "getdata.php",
type: "GET",
dataType: "json",
success: function(data) { for (var i = 0; i < data.length; i++) { markloc = new google.maps.LatLng(data[i].b, data[i].c); adddata(markloc); } }, error: function(data) { console.log( "error" ); } });
console.log("sucessful run of function");
}
function adddata(markloc){
marker = new google.maps.Marker({
position: markloc,
icon: 'http://www.wolfdoginfo.net/app/cropcircles.png',
map: map
});
deleteUserOverlay();
usermarker = marker;
}
我在控制台中收到错误,我爸爸现在输出如下
[{"name":"test2","lat":"39.8441792","lng":"-105.104921","speed":"bad","altitude":"dontcare","distance":"whatever"},{"name":"test","lat":"39.729431999999996","LNG":"-104.831919","speed":"speed","altitude":"altitude","distance"},{"name":"grant3","lat":">39.729431999999996","LNG":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant2","lat":"test34","lng":"test34","speed":"speed","altitude":"height","距离":"距离"},{"名称":"格兰特","纬度":"39.729431999999996","LNG":"-104.831919","速度":"速度","高度":"高度","距离":"距离"},{"名称":","纬度":"39.75198511","LNG":"-104.85021166","速度":"速度","高度","距离":"距离"}][{"名称":"test2","lat":"39.8441792","LNG":"-105.104921","速度":"坏","高度":"dontcare","距离":"随便"},{"名称":"测试","lat":"39.729431999999996","LNG":"-104.831919","速度":"速度","高度":"高度","距离":"距离"},{"名称":"Grant3","lat":"39.729431999999996","LNG":"-104.831919","速度":"速度","高度":"高度","距离":"距离"},{"名称":"Grant2","lat":"test34","LNG":"Test34","速度":"速度","高度":"高度","距离":"距离"},{"名称":"格兰特","纬度":"39.729431999999996","LNG":"-104.831919","速度":"速度","高度":"高度","距离":"距离"},{"名称":","纬度":"39.75198511","液化天然气":"-104.85021166","速度":"速度","高度":"高度","距离":"距离"}]
所以现在我的 PHP 由于某种原因只正确输出了两次但是我的 JSON 代码不起作用。我需要填充每个条目的标记。
缺点说:
[01:57:52.768] GET http://wolfdoginfo.net/app/show/getdata.php [HTTP/1.1 200 OK 86ms]
[01:57:52.705] "calluserlocation fires"
[01:57:52.705] "sucessful run of function"
[01:57:52.826] "error"
看起来您的 PHP 代码返回了无效的 JSON,因为它是一系列 json 字符串,而不是一个组合数组。
while($row = mysql_fetch_array($query)) {
echo json_encode(array( "a" => $row['name'], "b" => $row['lat'], "c" => $row['lng'], "d" => $row['speed'], "e" => $row['altitude'], "f" => $row['distance']));
}
// this code will return this JSON
// { a:?, b:?, c:?, ... }
// { a:?, b:?, c:?, ... }
// ...
您应该将它们组合成一个数组并返回它。
$return = array();
while($row = mysql_fetch_array($query)) {
$return [] = array( "a" => $row['name'], "b" => $row['lat'], "c" => $row['lng'], "d" => $row['speed'], "e" => $row['altitude'], "f" => $row['distance']));
}
echo json_encode($return);
// this code will return
// [
// { a:?, b:?, c:?, ... },
// { a:?, b:?, c:?, ... },
// ...
// ]
这是一个对象的数组,而不仅仅是松散的对象。
希望这有帮助!
您应该将所有详细信息推送到一个数组中,然后将该数组编码为 json 字符串。您可以使用如下代码来实现这一点:
$details_array = array();
while($row = mysql_fetch_array($query))
{
array_push($details_array, $row);//pushes all the fetched row data into $details_array
}
echo json_encode($details_array);