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]
...
3
  • 2
    Please stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy. Commented Jul 11, 2016 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 Commented Jul 11, 2016 at 18:23
  • I'm sorry for the deprecated functions ... unfortunately this is a project for academic purposes... Commented Jul 11, 2016 at 18:27

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

Comments

1

Use this

$.each(result, function(key, value){
        $.each(value, function(key, value){
            console.log(key, value);
        });
    });

1 Comment

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!
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);

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.