1

Firstly i'll point out the problem, basically inside my $.each(data, function (i, v) { code it's returning a bunch of variables which are echo'd from the PHP code which is using json_encode but when trying to place these variables or even alert() them it's showing undefined i've tried many ways to actually display the data, but it's always returning undefined and i don't know why since my code seems valid to my perspective.

The current javascript code i have is as follows

$.ajax({
        url: "functions/ajax.php",
        data: "func=auto",
        type: "GET",
        dataType: "json",
        success: function(data){
                $.each(data, function (i, v) {
                            var name = v['name'];
                            var player_id = v['id'];
                            alert(player_id);
                });
        },
        error: function (jqXHR, textStatus, errorThrown){
            console.log('Error ' + jqXHR);
        }
});

The current PHP code i have is as follows

$res = $DB->Query("SELECT * FROM `inventory` WHERE `account_id` = '$_SESSION[ID]'");
$data = array();
while($player = $DB->fetch_assoc()) {   
        $data['name'] = $player['name'];
        $data['id'] = $player['player_id'];
}
header('Content-type: application/json');
echo json_encode($data);

Just to sum the whole thing up, when using alert() on player_id it returns undefined in which obviously i want it to return the correct value

4
  • 2
    There is only one player in there, so don't .each() over it. Or run $data[] = array('name'=>$player['name'],'id'=>$player['player_id']); Commented Mar 13, 2013 at 21:47
  • Would that work because my while($player = $DB->fetch_assoc()) { is returning more than one result ? Commented Mar 13, 2013 at 21:50
  • 1
    Look at what $data is in your current script. It will only be last player in the list unless you append records to $data instead of overwriting it. Commented Mar 13, 2013 at 21:53
  • Ah, well, @MarcB took the time to explain it more in depth. Commented Mar 13, 2013 at 21:54

1 Answer 1

6

You're not sending back a multi-level array, you're sending an array with just two elements. e.g. your JS code should be

    success: function(data){
               var name = data['name'];
               var player_id = data['id'];
             }

If that DB query IS supposed to send back multiple records, then you're building it wrong:

while($player = $DB->fetch_assoc()) {   
   $data[] = array('name' => $player['name'], 'id' => $player['player_id']);
}
echo json_encode($data);

and then your $.each() code should start working, as you ARE sending back a multidimensional array/object.

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

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.