0

i'm creating a map with google maps api.

Basically, the array structure to call any markers is like this:

var station = [
['SEDRANO DI SAN QUIRINO (PN)',46.048887,12.657924,'Via Maniago, 28','Tel. 0434 918900','sedrano-di-san-quirino.jpg',true,true,false,false,false,false,false],
['CORTINA D\'AMPEZZO (BL)',46.541307,12.132348,'Via dello Stadio, 7','Tel.  0436 869133','cortina-dampezzo.jpg',true,true,false,false,false,true,false],
['POINCICCO DI ZOPPOLA (PN)',45.948725,12.745332,'Via Cusano, 14','Tel. 0434 574831','poinciccio-di-zoppola.jpg',true,true,false,false,false,true,false]
];

Now, i would create an array with json calls, so... first step i call markers with php and put these in json_encode

$sth = mysql_query("SELECT * FROM markers");
$rows = array();
 while($r = mysql_fetch_assoc($sth)) {
  $rows[] = $r;
 }
print json_encode($rows);

result

[{"city":"SEDRANO DI SAN QUIRINO (PN)","lat":"46.0489","lng":"12.6579","address":"Via Maniago, 28","phone":"Tel. 0434 918900","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/sedrano-di-san-quirino.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"false","cafe":"false","verde":"true","diesel":"true","id":"1"},{"city":"CORTINA D'AMPEZZO (BL)","lat":"46.5413","lng":"12.1323","address":"Via dello Stadio, 7","phone":"Tel. 0436 869133","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/cortina-dampezzo.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"true","cafe":"false","verde":"true","diesel":"true","id":"2"},{"city":"POINCICCO DI ZOPPOLA (PN)","lat":"45.9487","lng":"12.7453","address":"Via Cusano, 14","phone":"Tel. 0434 574831","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/poinciccio-di-zoppola.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"true","cafe":"false","verde":"true","diesel":"true","id":"3"}]

secondo step i call with jquery getJSON datas, like this...

$.getJSON( "markers.php", function( data ) {
    var station = [];
  $.each( data, function( key, val ) {
    station.push( data );
  });
});

But this is totally out of work, so... in which way i could do it? So... is there a way to accomplish this structure?

  var station = [
    ['SEDRANO DI SAN QUIRINO (PN)',46.048887,12.657924,'Via Maniago, 28','Tel. 0434 918900','sedrano-di-san-quirino.jpg',true,true,false,false,false,false,false],
    ['CORTINA D\'AMPEZZO (BL)',46.541307,12.132348,'Via dello Stadio, 7','Tel.  0436 869133','cortina-dampezzo.jpg',true,true,false,false,false,true,false],
    ['POINCICCO DI ZOPPOLA (PN)',45.948725,12.745332,'Via Cusano, 14','Tel. 0434 574831','poinciccio-di-zoppola.jpg',true,true,false,false,false,true,false]
    ];
1
  • according to jsonlint.com your goal isn't a valid json object. Commented Jul 9, 2014 at 14:32

2 Answers 2

1
var stations = []; //You need an external variable to hold all the stations

$.getJSON( "markers.php", function( data ) {
    //var station = []; //Don't need this here

    //"data" is an array, not a key/value object
    /*$.each( data, function( key, val ) {
        //You are adding the JSON "data" array here, not the values
        station.push( data );
    });*/

    //Remember "data" is a JSON array, not object
    for (var i = 0; i < data.length; i++) { 
        var station = []; //Temporary variable to hold station data

        //Iterate over the current station object
        $.each(data[i], function(key, value) { 
          station.push(value); //You're not interested in the key
        });
        stations.push(station); //Add station to array of all stations
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Don't fetch associative arrays if you only care about the array value, try this:

$rows = array();
 while($r = mysql_fetch_array($sth)) {
  $rows[] = array_values($r);
 }
print json_encode($rows);

Comments

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.