问题描述:

I am using codeigniter. Here is my php code to query result in array of obj then how can i pass them to js. I wanna use it in google maps

 <?php

foreach ($query->result() as $row)

{

$lat = $row->geo_lat;

$lng = $row->geo_lng;

$pic = $row->picture;

echo $lat;

echo $lng;

echo "<img src=\"" . base_url() . $pic . "\"/><br />";

//echo json_encode($row);

//echo "<pre>";

//echo print_r($row);

}

?>

Js code

var locations = ?????????;

var marker, i;

for (i = 0; i < locations.length; i++) {

marker = new google.maps.Marker({

position: new google.maps.LatLng(locations[i][0], locations[i][1]),

map: map,

draggable: true

});

}

网友答案:

You can emit Javascript with PHP...why don't you just JSON-encode all of the results and store them in your locations variable, like so:

<?php 
    echo( "<script>" );
    echo( "  var locations = " + json_encode($query->result()) + ";" );
    echo( "</script>" );
?>
网友答案:

You can use it with or without ajax.

You can simply have overall result in json format using json_encode() _and add it to js variable like:

<script>
    var js_obj=<?php echo  json_encode($result); ?>
  /* now you can access this json object that is in js_obj variable and acccess any thing you want using dot operator.*/
</script>

Your question is not much clear so if you are asking in some different sense then please be clear so that I can focus on actual problem.

网友答案:

You need to use AJAX to have your JS code to ask for the PHP page. The PHP page will process the request, and return appropriate data in JSON format (using json_encode() on the result object).

相关阅读:
Top