timmah1 Posted December 19, 2016 Share Posted December 19, 2016 This is something small that I'm forgetting, but I'm lost now. I need to get the information from the db to display as follows: var markersData = [ { lat: 40.6386333, lng: -8.745, name: "xxxx", address: "xxxx", place: "xxxxx", hours: "xxxxx", location: "xxxx" }, { lat: 40.59955, lng: -8.7498167, name: "xxxx", address: "xxxx", place: "xxxxx", hours: "xxxxx", location: "xxxx" }, { lat: 40.6247167, lng: -8.7129167, name: "xxxx", address: "xxxx", place: "xxxxx", hours: "xxxxx", location: "xxxx" } ]; Now, my query is this: $sql = "SELECT * FROM events"; $result = $conn->query($sql); while($row = $result->fetch_assoc()) { $events[] = array( 'lat' => $row['event_lat'], 'lng' => $row['event_lng'], 'name' => $row['event_name'], 'address' => $row['event_address'], 'place' => $row['event_place'], 'hours' => $row['event_hours'], 'location' => $row['event_location'] ); echo '<pre>'.print_r($events); } Which prints out this: Array ( [0] => Array ( [lat] => [lng] => [name] => Lobster Louie's Truck [address] => 300 Pine, San Francisco, CA 94104 [place] => 300 Pine [hours] => 8:00am - 10:00am [location] => Located at the corner of pine and 3rd. ) ) Array ( [0] => Array ( [lat] => [lng] => [name] => Lobster Louie's Truck [address] => 300 Pine, San Francisco, CA 94104 [place] => 300 Pine [hours] => 8:00am - 10:00am [location] => Located at the corner of pine and 3rd. ) [1] => Array ( [lat] => [lng] => [name] => Lobster Louie's Truck [address] => Terry Francois Blvd, San Francisco, CA [place] => The Yard at Mission Rock [hours] => 11:00am - 3:00pm [location] => Located at the Yard ) ) I do not understand why it's showing 3, when there is only 2 in the database, but regardless, it's not working anyhow. Can somebody please see what's wrong so that it formats correctly? Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 19, 2016 Share Posted December 19, 2016 (edited) What you are after is a JSON encoded array. http://php.net/manual/en/function.json-encode.php Plus, it is showing three entries because you have the print_r() within the loop that is creating the records. So, on the first iteration you add the first element to the array and output the array (with just that one element). Then on the second iteration of the loop, you add a second element to the array and output the array again (which now holds two elements). 1 + 2 = 3. move the print_r() outside the loop to see the final contents of the array. Edited December 19, 2016 by Psycho Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 19, 2016 Author Share Posted December 19, 2016 So, essentially it would be this: $sql = "SELECT * FROM events"; $result = $conn->query($sql); while($row = $result->fetch_assoc()) { $events[] = array( 'lat' => $row['event_lat'], 'lng' => $row['event_lng'], 'name' => $row['event_name'], 'address' => $row['event_address'], 'place' => $row['event_place'], 'hours' => $row['event_hours'], 'location' => $row['event_location'] ); } echo json_encode($events); to output like so { lat: 40.6386333, lng: -8.745, name: "xxxx", address: "xxxx", place: "xxxxx", hours: "xxxxx", location: "xxxx" }, { lat: 40.59955, lng: -8.7498167, name: "xxxx", address: "xxxx", place: "xxxxx", hours: "xxxxx", location: "xxxx" }, { lat: 40.6247167, lng: -8.7129167, name: "xxxx", address: "xxxx", place: "xxxxx", hours: "xxxxx", location: "xxxx" } Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 19, 2016 Author Share Posted December 19, 2016 Maybe I'm doing this entire thing wrong. The code I'm using is this: var markersData = [ <?php $sql = "SELECT * FROM events"; $result = $conn->query($sql); while($row = $result->fetch_assoc()) { $events[] = array( 'lat' => $row['event_lat'], 'lng' => $row['event_lng'], 'name' => $row['event_name'], 'address' => $row['event_address'], 'place' => $row['event_place'], 'hours' => $row['event_hours'], 'location' => $row['event_location'] ); } echo json_encode($events); ?> ];Is this not the proper way to do this?I'm using php on the javascript file Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 19, 2016 Share Posted December 19, 2016 And what is the current output with the above code? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 19, 2016 Author Share Posted December 19, 2016 Like this: [{"lat":"","lng":"","name":"Lobster Louie's Truck","address":"300 Pine, San Francisco, CA 94104","place":"300 Pine","hours":"8:00am - 10:00am","location":"Located at the corner of pine and 3rd."},{"lat":"","lng":"","name":"Lobster Louie's Truck","address":"Terry Francois Blvd, San Francisco, CA","place":"The Yard at Mission Rock","hours":"11:00am - 3:00pm","location":"Located at the Yard"}] But the map isn't populating, so I guess I was just assuming that the php code is at fault Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 19, 2016 Author Share Posted December 19, 2016 I've never heard of this or seen this before, but "inspecting" the code, I'm getting this now Uncaught RangeError: Maximum call stack size exceeded What is that? Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted December 19, 2016 Solution Share Posted December 19, 2016 (edited) So, what's the problem? That is correct JSON format. If your issue is with the empty fields (lat & lng) those were already empty in your previous output, so converting to JSON will not put values there. Either the fields (event_lat & event_lng) are empty in those records or those are not the correct field names. Here's that same data in a more readable format [ { "lat":"", "lng":"", "name":"Lobster Louie's Truck", "address":"300 Pine, San Francisco, CA 94104", "place":"300 Pine", "hours":"8:00am - 10:00am", "location":"Located at the corner of pine and 3rd." }, { "lat":"", "lng":"", "name":"Lobster Louie's Truck", "address":"Terry Francois Blvd, San Francisco, CA", "place":"The Yard at Mission Rock", "hours":"11:00am - 3:00pm", "location":"Located at the Yard" } ] Edited December 19, 2016 by Psycho 1 Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 19, 2016 Author Share Posted December 19, 2016 Thank you for your help, but I'm getting different errors now that I've never seen before Uncaught RangeError: Maximum call stack size exceeded And I have no idea how to fix that. Even reading about it, i'm totally clueless Thank you for your help, it is really appreciated! Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 19, 2016 Share Posted December 19, 2016 I can't see that that error would have anything to do with defining a two element JSON array. Based on a couple quick searches it could be caused by an infinite loop in the JavaScript or the redefining of elements within a loop. Just like you were trying to output the array on each iteration of the PHP loop, check that you aren't doing something similar in the JS code. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.