Reputation: 4740
I am modifying some javascript for Google maps so that the different pin information is retrieved from my database. If I was to do this with just php it would be like this:
<?php while($row = mysql_fetch_assoc($query))
{$title = $row['title']; $desc = $row['desc']; echo 'This '.$title .' and this '. $desc .';}?>
If there were 5 rows in the database there will be five lines with 5 different titles and descriptions. But I have this line of javascript that I would like to be in a while loop:
map.addMarkerByLatLng(37.8685, -1.5108, "Sanatario de Terburculosos", createInfo("title goes here",""));
How can i write it so that that javascript is in a php while loop or how can i do the javascript equivelant of the php while loop so that i can retrieve multiple rows of lang,lat info each in its own javascript code like above?
Thank you.
Upvotes: 3
Views: 101
Reputation: 10341
Combine the two code examples you have provided:
<?php
$pinTpl = 'map.addMarkerByLatLng( %s , %s , "%s" , createInfo( "%s" , "" ) );';
while( $row = mysql_fetch_assoc( $query ) ){
echo sprintf( $pinTpl ,
$row['lat'] , # Where 'lat' is the field name for the latitude in DB
$row['long'] , # Where 'long' is the longitude field name in DB
$row['title'] , # The title
$row['desc'] # The description
)."\n"; # A newline, for readability
}
?>
Upvotes: 1
Reputation: 18557
ajax.php
<?php
$obj = array();
while($row = mysql_fetch_assoc($query)){
$obj[] = new array(
"title" => $row["title"],
"desc" => $row["desc"],
"lat" => $row["lat"],
"lon" => $row["lon"]
);
}
echo json_encode($obj);
?>
jquery ajax
$.getJSON("ajax.php",function(data){
for(var i=0;i<data.length;i++){
map.addMarkerByLatLng(
data[i].lon,
data[i].lat,
data[i].description,
createInfo(data[i].title,""));
}
});
Upvotes: 2