Reputation: 29
I don’t know how to wirte the PHP to go with my JSON JavaScript code. I assume I need it to parse each row into a main array that assigns each one a unique value key starting with 1 to infinity. However, honestly I’ve looked and I don’t get how to do it. Also, if you see anything wrong with the mapping adding marker, please let me know.
PHP CODE:
<?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 CODE:
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;
}
I get errors in the console and my dad now outputs like this
[{"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":"distance"},{"name":"grant3","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant2","lat":"test34","lng":"test34","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"","lat":"39.75198511","lng":"-104.85021166","speed":"speed","altitude":"altitude","distance":"distance"}][{"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":"distance"},{"name":"grant3","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant2","lat":"test34","lng":"test34","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"","lat":"39.75198511","lng":"-104.85021166","speed":"speed","altitude":"altitude","distance":"distance"}]
so now my php is outputting correctly just twice for somereason but my json code isnt working. I need to populate the marker for each entry.
consays:
[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"
Upvotes: 0
Views: 396
Reputation: 1722
Looks like your PHP code is returning invalid JSON, because it is a series of json strings, instead of one combined array.
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:?, ... }
// ...
You should combine them into one array and return it.
$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:?, ... },
// ...
// ]
Which is an array of objects, instead of just loose objects.
Hope this helps!
Upvotes: 3
Reputation: 10161
You should push all the details into an array and then after encode that array in to json string. You can achieve that by using code like :
$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);
Upvotes: 0