...

View Full Version : Need Help with passing mysql/php array to javascript



ArnMan
09-17-2011, 04:26 AM
hello,
I have spent nearly the past week scouring the net, looking for a solution. I am needing to query mysql with php, with gives me a array of information.
And I need to feed, this info into my javascript google maps api
now before you say anything, my google map is working, i just want to add multiple markers.
And the line I believe that needs to be changed, is just a plain javascript.

first i wil start off and show my mysql query.

$cityl=mysql_query("SELECT * FROM citylocation WHERE citylong < {$ir['userlongitute']}+5 AND citylong > {$ir['userlongitute']}-5 AND citylat > {$ir['userlatitude']}-5 AND citylat < {$ir['userlatitude']}+5 ");
$cityarray = mysql_fetch_array( $cityl);

the npclocation table has the following fields w/descriptions

citylocid - autoincrement
cityid - id for cities
citylname - name for the cities
citylong - longitude for cities
citylat - latitude for cities

inside the javascript I have

var newpoints = new Array();

and here is the function inside the javascript

function addPoints() {

newpoints[0] = new Array(51.45400691005981, -2.63671875, icon0, 'bristol', 'bristol');
newpoints[1] = new Array(53.30462107510271, -6.1962890625, icon0, 'dublin', 'dublin');
newpoints[2] = new Array(30.031055426540206, -90, icon0, 'New Orleans', 'New Orleans');

for(var i = 0; i < newpoints.length; i++) {
var point = new GPoint(newpoints[i][1],newpoints[i][0]);
var popuphtml = newpoints[i][4] ;
var marker = createMarker(point,newpoints[i][2],popuphtml);
map.addOverlay(marker);
}
}

As you can see there are numerous newpoints[somenumber] and array info.
I dont want to have to have lines like that, i want to integrate the table fields array into the line and then i guess looped through until the end of the line.
to something like

newpoints[?] = new Array(citylong, citylat, icon0, 'citylname', 'citylname');

or something like that
I am terrible at js skills
my php skills could be better, but I can get the job done in php.

Any help would be great appreciated.

sunfighter
09-17-2011, 02:34 PM
Normally you pass php variables to js like this:

var jsVar = <?php echo '"'.$phpVar.'"'; ?>;
That's two single quotes around a double quote, in both places.

ArnMan
09-17-2011, 03:30 PM
is that for a single variable or an array? I have been able to get single php variables in js, barely lol.

I am looking to fetch an array of data from mysql and put that array into the javascript function

BluePanther
09-18-2011, 08:37 AM
All you need to do, is set up a while for your database mysql_fetch_array (or a foreach once you've formed the array outside this while) to output the values into the array. Use the information you know - you know how to set up an array in javascript, and you know how to loop through an array in PHP. You just need an incrementing counter (this is based on your example btw) that will increment the javascript array's index, and input the values based on that. You've just got to remember that PHP and JS are separate - you're basically getting PHP to output the javascript code for the browser to run, not directly running the javascript with the PHP.

An example:


// Echo the start of the function
// The two new lines and the tab (\n\n\t) at the end are just for ease of reading the code
// It should match your current code's layout (maybe haha). It's completely optional
echo 'function addPoints() {'."\n\n\t";

// Set a counter
$i=0;

// Go through every row, echo the javascript code as you go
while($cityarray = mysql_fetch_array($cityl)){
echo "newpoints[".$i."] = new Array('".$cityarray['information']."');\n\t";
$i++;
}

// echo rest of function
echo 'for(var i = 0; i < newpoints.length; i++) {
var point = new GPoint(newpoints[i][1],newpoints[i][0]);
var popuphtml = newpoints[i][4] ;
var marker = createMarker(point,newpoints[i][2],popuphtml);
map.addOverlay(marker);
}
}';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum