2

I need to add some geo-marker to my map. the markers are in my mysql table on altervista.org but my JavaScript says [object Object] every time i try...

here my php code:

require('connect.php');
$query = "SELECT latit, longit FROM segnalazioni";
$result = mysql_query($query);
$rows = array();
while ($row = mysql_fetch_assoc($result)){
     $rows[] = $row;
}
echo json_encode($rows);

it returns:

[{"latit":"12.34","longit":"12.34"},{"latit":"56.78","longit":"56.78"},...]

here my JavaScript:

function addMarker(mapobj) {
    $.getJSON( "http://####.altervista.org/map.php", function( data ) {
        var items = [];
        $.each( data, function( key1 , val1 ) {
            items.push( "<li id='" + key1 + "'>" + val1 + "</li>" );
                //next todo:
                //mapobj.marker([latit, longit]).addTo(map).bindPopup("hi");
            });
        $( "<ul/>", {
            "class": "my-new-list",
            html: items.join( "" )
        }).appendTo( "body" );
    });
}

and on the end of my [body] i can see only:

[object Object]
[object Object]
[object Object]
...
  • 2
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 11 '16 at 18:21
  • 2
    you have an array of objects. your `data` is looping on the array, which means `key1` is simply `0`, `1`, etc... and `val1` is an OBJECT containing your lat/long values, not values themselves – Marc B Jul 11 '16 at 18:23
  • I'm sorry for the deprecated functions ... unfortunately this is a project for academic purposes... – Eros Scoppiato Jul 11 '16 at 18:27

3 Answers3

1

Use this

$.each(result, function(key, value){
        $.each(value, function(key, value){
            console.log(key, value);
        });
    });
Suman
  • 401
  • 5
  • 15
  • ok, it's works... but... WHY? I tried to change the query in 'select latit from...' so the results were only one at a time in json... but doesn't works anyway! – Eros Scoppiato Jul 11 '16 at 18:47
1

I would also encourage to use header before sending JSON stream. It is always good to tell the content type sent in HTTP response. Use

header('Content-Type: application/json');

Before using

echo json_encode($rows);
Atul Jindal
  • 946
  • 8
  • 8
1

According to jquery.each the parameters are

  • indexInArray , value and NOT key, value

So the code is:

$(function () {
  var data = [{"latit": "12.34", "longit": "12.34"}, {"latit": "56.78", "longit": "56.78"}];
  var items = [];
  $.each(data, function(indexInArray , value) {
    items.push( "<li id='" + indexInArray + "'>latit: " + value.latit + ' longit:' + value.longit + '  OR '+ JSON.stringify(value) + "</li>" );
    //next todo:
    //mapobj.marker([latit, longit]).addTo(map).bindPopup("hi");
  });
  $( "<ul/>", {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "body" );
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
gaetanoM
  • 41,594
  • 6
  • 42
  • 61