On Oct 22, 1:06 pm, phicarre <[email protected]> wrote:
> To Pavel: the context is different: I want to pass data to javascript
> function. The function is not in a waiting state ! I execute the
> function from PHP with echo.
> To Jake: I transform a string in array.
> How to execute a javascript function from PHP with parameters coded
> with json_encode ?
>
You can execute a javascript function with data from a PHP array
encoded via jason_encode. Here's how (untested code! replace
do_query and get_row with the functions you use):
$results = do_query("SELECT lat, lon, name, otherinfo FROM
tablename");
$points = array();
while ($row = get_row($results)) {
$points[]=$row;
}
$encoded_points = json_encode($points);
echo "
<script type='text/javascript'>
function make_markers(points) {
var i;
for (i=0; i< points.length; i++) {
gpoint = new GLatLng(points[i].lat, points[i].lon);
// create marker her with points[i].name and points
[i].otherinfo
// etc.
}
}
var points = {$encoded_points};
make_markers(points);
</script>
";
Since the DB returns results in that "(1.1,9.4),(1.15,9.4),(1.2,9.35)"
format, you will have to take the points and put them in an array
yourself.
Option 2:
Like a previous poster mentioned, If it's too much trouble to get each
lat lon (or whatever) pair individually and put them in a PHP array,
another option is to encode the string it in JavaScript Object
Notation your self.
You can:
replace
( with [
) with ]
add a [ to the left end of the string
and add a ] to the right end of the string
and then you've got a json encoded object that you can assign directly
to a JS variable.
[ [10,20],[30,40]]
points[0][0] is lat (or whatever the first number in the coordinates
is) of the first point
points[0][1] is lon of the first point
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-maps-api?hl=en
-~----------~----~----~----~------~----~------~--~---