I have really done what I think is a not-code-review-passing hack to pass a 2-dimensional array from PHP to Javascript. The outer array has elements, and each of its elements is an array.
Each inner array consists of 4 fields from my database records. The inner array elements are: the name of a town, its latitude and its longitude, and an integer index.
Is the code working? Yes. But I'm hating the fact that I am 99% convinced that, as a fairly raw beginner, there is a much better way to create then pass a 2d array from PHP to Javascript and I need to know how, as I hacked this together through trial-and-error and reading lots of SO posts, and did not create this from a priori know-how, wisdom or confidence.
Each outer array element is an array that (needs to) look like this:
top array:
[0] = ["Campbell", 37.21, 122.0, 0]
[1] = ["Sunnyvale", 37.54, 121.37, 1]
[2] = ["Saratoga", 37.24, 122.001, 2]
[3] = ......etc. etc...........
Note that the 2nd level arrays have a string, then a float, then another float, then an integer.
Here's the code in PHP that packages my database records into a 2-d PHP array (error checking, other code is not shown, for clarity):
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
$outerArray = array();
for($i = 0; $i < $numrows; $i++)
{
$theRow = mysql_fetch_row($result);
$city = $theRow[1];
$lat = $theRow[22];
$lng = $theRow[23];
$outerArray[] = array($city, $lat, $lng, $i);
}
$latLngValues = json_encode($outerArray);
// EDIT: ADDED TO TELL ME WHAT THIS 2d ARRAY ACTUALLY LOOKS LIKE
var_dump($latLngValues);
In the onload handler for my web page, I call a javascript function and pass this array:
<body onload='handleLoad(<?php echo $latLngValues ?>)'>
Here is my handleLoad():
function handleLoad( latLng2dArray )
{
for (var i = 0; i < latLng2dArray.length; i++)
{
var town = latLng2dArray[i];
var latitude = Number(town[1]);
var longitude = Number(town[2]);
// I USE THE DATABASE RECORD'S NUMBERS TO CREATE A Gmaps LatLng
var myLatLng = new google.maps.LatLng(latitude, longitude);
}
There's probably a cleaner, and/or more efficient, way to do this.
For example, I found that if I did not use the Number() function to 'force' my database numbers into a number, the call to "new google.maps.LatLng(latitude, longitude)" was giving me NaN.
So I'm hoping more experienced people can let me know a cleaner way. While this code works, I hacked at it for a day through trial and error.
EDIT: I used a var_dump() in my php code to see what the 2d array "$latLngValues" looks like just before it's passed to my onload handler, and "$latLngValues" looks like this:
string(133) "[["Campbell","37.2724","-122",0],["Sunnyvale","37.2724","-122",1],["LosGatos","37.2607","-122",2],["Saratoga","37.2607","-122.046",3]]"
I want to point out this: in my database, the data type I used is 'float' for the latitude and longitude. Not sure how/why the var_dump shows them as a string (not floats) while the integer index is treated correctly, and not a string.
$latLngValues
actually contain? Have you inspected it? Maybe when you do, the cause will be obvious.$lat
and$lng
before adding them to the array?mysql_fetch_row
, it seems to return all strings