Php variable obj to js

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

    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:

    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:

    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.*/

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).

Need Your Help

Sorting Array of Pointers in C++

c++ arrays algorithm sorting pointers

Hoping I can get a little advice on a sorting method I made.

Build maven assembly in java 8

java-8 maven-assembly-plugin

I changed in my Project the version of java used. Now i'm using lambdas expressions.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.