Jump to content

array messed up


timmah1
Go to solution Solved by Psycho,

Recommended Posts

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

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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"
   }
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Solution

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 by Psycho
  • Like 1
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.