Jump to content

How best do I implement mysql data into a line of javascript code?


final60

Recommended Posts

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.

map.addMarkerByLatLng(37.8685, -1.5108, "Sanatario de Terburculosos", createInfo("<?php echo $title; ?>",""));

 

Puts $title where you want it but some code changes are needed to get all 5 changes into the js.

 

Also a small correction to your code:

<?php 
while($row = mysql_fetch_assoc($query))
{
$title = $row['title']; 
$desc = $row['desc']; 
echo 'This '.$title .' and this '. $desc; <<== Removed the .'
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.