3

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.

2 Answers 2

2

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,""));
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

if you start using code like this, you'll be setting yourself up for failure. You're better off doing something like echo 'var='.json_encode($data).';' and accessing that variable.
@ilia choly: I disagree, but am intrigued by your reasoning. What makes you think this is "setting yourself up for failure"?
because you're generating javascript with php. That's extremely hacky. You could at least have a separate js file for generating and loading all required variables as objects created with json_encode. Finally, the code you're generating is very redundant as opposed to iterating over an object.
@ilia choly: But it's simple, answers the question with the least amount of reworking, does not add an AJAX call to the process, and provides the OP with a stepping stone towards developing more complex systems (and using more complex systems to do so). I get what you are saying, but still, I disagree with the basis of your arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.