xinbing89
xinbing89

Reputation: 11

php retrieving multiple arrays of json objects

I am sending an array of JSON objects into the database. They are saved as this:

function send_custom_markers() {
    var reponse = confirm("Send markers to selected contacts?");
    if (reponse === true)
    {
        var json_markers = new Array();
        for (var i = 0; i < myCustomMarkers.length; i++) {
            json_markers.push({
                title: myCustomMarkers[i].getTitle(),
                lat: myCustomMarkers[i].getPosition().lat(),
                lng: myCustomMarkers[i].getPosition().lng(),
                img: myCustomMarkers[i].getIcon(),
            });
        }

        json_markers = JSON.stringify(json_markers);

        $.ajax({
            type: 'post',
            url: 'dist/backend/action_controller.php',
            data: {action: "send_custom_markers", userID: userID, data: json_markers},
            success: function() {
                alert("Markers Sent");
            }
        });
    }
}

I know this part works because in the database they look like this:

[{"title":"Marker: 1","lat":37.6637114...

Now I need to get them all out.

Php code:

$result = $this->query($sql);
$friends_markers = array();
while ($row = $result->fetch_array()) {
    $friends_markers[] = json_encode($row, true);
    //Something needs to go here.
}
echo $friends_markers;      

And JavaScript code:

function get_friends_custom_markers(userID) {
    $.ajax({
        type: 'post',
        url: 'dist/backend/action_controller.php',
        dataType: 'json',
        data: {action: "get_friends_custom_markers", userID: userID},
        success: function(friends_markers) {
            alert(friends_markers[0][0].title);
        },
        error: function(xhr, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}  

I put the alert there just to test what comes out.

My logic is that for every user, they have an array of JSON objects saved. And to get them all out, I need an array whose every element is an array of JSON objects. So if I do:

alert(friends_markers[0][0].title);

The output should be:

"Marker: 1"

But instead I get

200
parsererror

I know the everything works up until $result. And I know the problem is in that while loop in the php function. I have tried many different things but none works. And this has taken me days. I am new to php and JavaScript. This is the last part of a project that I am working on. Once I can put then all in an array, then I can draw them all onto Google maps. So anything will be helpful. Thanks.

Upvotes: 1

Views: 349

Answers (1)

dgersting
dgersting

Reputation: 11

You should be able to get a better idea of what's coming back out of your ajax call by dumping friends_markers to the browser's debug console with console.log(friends_markers) inside your success() callback. All the current browsers ship with the console, though getting to it is different for each.

Upvotes: 1

Related Questions