user3155074
user3155074

Reputation: 1161

how to use jquery with array of information coming from php

I'm looking for a way to properly read an array coming from a php script:

php:

$query="SELECT *, UNIX_TIMESTAMP(TIME) as epoch_time FROM node WHERE netid='$netid'";
$result=mysql_query($query, $conn);

// Plot our nodes
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{

    $longitude=$row["longitude"];
    $latitude=$row["latitude"];

    $points[] = array('lat' => $latitude, 'lng' => $longitude);


}

mysql_close($conn);
print_r ($points);

the result of script when called in standalone:

Array ( [0] => Array ( [lat] => 35.91204361476439 [lng] => 39.05123084783554 ) [1] => Array ( [lat] => 36.91204361476439 [lng] => 38.05123084783554 ) [2] => Array ( [lat] => 33.33 [lng] => 34.44 ) [3] => Array ( [lat] => 35.78 [lng] => 33.2 ) [4] => Array ( [lat] => 37.000000 [lng] => 32.3456 ) )

Now I want to manage by ajax this array, getting the pair of latitude and longitude for each element in the array I have the following idea:

function loadPoints(){

    $.ajax({
        url:'markers.php',
        success:function(points){
        var markers = {};
        markers = points;
        for (var i in markers) {
        $.each(markers[i], function(key, val){
        var position = [val.lat, val.lng];
        console.log(position);
        });
        }
       }
    });

but it is not working. Any idea How can I manage this array?

Thank you

Upvotes: 0

Views: 60

Answers (4)

Quentin
Quentin

Reputation: 943157

print_r is designed for outputting data for debugging purposes. It isn't a standard data format, and jQuery has no special rules for handling it (especially if you tell it that you are sending it HTML).

So:

  1. Tell the client that you are sending JSON: header("Content-Type: application/json");
  2. Output JSON: print json_encode($points);

Then:

success:function(points){
    for (var i = 0; i < points.length; i++) {
        var marker = points[i];
        // etc

Note that I removed:

var markers = {};
markers = points;

since creating an empty object, then immediately overwriting it is pointless. Just use the variable you already have.

Upvotes: 1

Louis XIV
Louis XIV

Reputation: 2224

Use json_encode in you PHP (instead of print_r).

and you can use $.getJson:

$.getJSON( 'markers.php', function( data ) {
    $.each( data, function( key, val ) {
        var position = [val.lat, val.lng];
        console.log(position);
    });
});

Upvotes: 0

Sam
Sam

Reputation: 20486

The result of the script called in standalone is exactly what jQuery will be reading. So var points = 'Array ( [0] => Array ( [lat] => 35.91204361476439 [lng] => 39.05123084783554 ) [1] => Array ( [lat] => 36.91204361476439 [lng] => 38.05123084783554 ) [2] => Array ( [lat] => 33.33 [lng] => 34.44 ) [3] => Array ( [lat] => 35.78 [lng] => 33.2 ) [4] => Array ( [lat] => 37.000000 [lng] => 32.3456 ) ). This is a prime example of when to use JSON.

In markers.php, change print_r($points); to echo json_encode($points);. And then modify your jQuery slightly to expect a JSON object as a response:

$.ajax({
    url:'markers.php',
    dataType: 'json',
    success: function(points) {}
});

Upvotes: 1

Binary Alchemist
Binary Alchemist

Reputation: 1620

Replace print_r with json_encode. json_encode will encode the php array as JSON.

echo json_encode( $points );

Upvotes: 0

Related Questions