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.

Link to comment
Share on other sites

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 .'
}
?>

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.