Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. foreach ($parks as $key => $val) {
  2. echo $key;
  3. };
  4.  
  5. <?php
  6. $dbname = "DATABASE";
  7. $password = "PASSWORD";
  8. $servername = "localhost";
  9. $dbname = "NAME";
  10.  
  11. $conn = new mysqli($servername, $username, $password, $dbname);
  12.  
  13. $query = "SELECT name, latitude, longitude, description, where_to_go from locations";
  14. $result = mysqli_query($conn, $query);
  15.  
  16. $parks = [];
  17.  
  18. while($row = mysqli_fetch_array($result)){
  19. $park = new stdClass();
  20. $location = new stdClass();
  21. $location->lat = floatval($row['latitude']);
  22. $location->lng = floatval($row['longitude']);
  23. $park->title = $row['name'];
  24. $park->location = $location;
  25. $park->description = $row['description'];
  26. $park->whereToGo = $row['where_to_go'];
  27. array_push($parks, json_encode($park));
  28. };
  29. /*
  30. foreach ($parks as $key => $val) {
  31. echo $key; //THIS WILL WORK
  32. };
  33.  
  34. foreach ($parks as $key => $val) {
  35. echo $val; //THIS WILL NOT RETURN ANYTHING
  36. };
  37.  
  38. echo $parks[0] //SIMILARLY, THIS WILL NOT RETURN ANYTHING
  39. */
  40.  
  41. $conn->close();
  42. ?>
  43.  
  44. <script type="text/javascript">
  45. var locations = <?php echo json_encode($parks); ?>;
  46. var locations = locations.map(JSON.parse);
  47. console.log(locations);
  48. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement