0

Im currently trying to do the follow:

  1. Request a PHP file from my image.js code
  2. In the request call - query out data from my mysql database and save it in a PHP array
  3. Return the array to image.js as a JSON object.

I got nr 1 + nr 3 covered - what im strugling with is how to save my database attributes correctly into the PHP array and afterwards iterate through each record from the json callback.

Database attribute example:

player_id (unique key) || player_name || player_country || player_image || player_league ||

Question/Challenge 1: Saving the Array (this is what im not sure of)

while ($row = mysql_fetch_assoc($res))
{
    $myCallbackArray[] = array($row['player_id'], $row['player_name'], $row['player_country'], $row['player_image']);
}

- The following array, will just be one "flat-array" with no dimension based on saving all corresponding attributes under seperate player_id's?

To give some some context - and assuming the array is fine, we then in a 'next-step' send it back to JS

$callback = $myCallbackArray;
echo json_encode(array('returned_val' => $callback));

Question/Challenge 2: Accessing the array values in JS (this is what im not sure of)

      //Save the data
    var url = "request.php"; //

      var request = $.ajax({
             type: "POST",
             url: url,
             dataType: 'json',
             data: { user_id: id},

             success: function(data)
             {

//HERE WE HANDLE THE RETURNED ARRAY
if(data.returned_val) {

                  for( var i = 0; i < data.returned_val.length; i++ ){
//HERE I WOULD LIKE TO MAKE THE DIFFERENT ATTRIBUTES ACCESSABLE
                  } 
             },
             error:function() {
                //FAILURE
            }   

});
return false;

-So in this part im not sure how to actually handle the multi-dimensional array from PHP. I assume we need to save it out in a Javascript array and then we can probably iterate / access each value through an foreach loop - but yet again,- how im not entirely sure?

user1231561
  • 3,239
  • 6
  • 36
  • 55
  • For the javascript part, basically traverse arrays with `for(;;)` loops, and objects with `for..in` loops. All the info you need is here: http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – bfavaretto Aug 26 '13 at 21:43
  • I think this should answer your question ! Sorry i can't comment rep isn't high enough yet !https://stackoverflow.com/questions/15914282/php-encode-json-2-dimensional-array – Pablo208 Aug 26 '13 at 21:42

2 Answers2

0

I'll suggest to use json_encode:

$myCallbackArray []= (object) array(
    "player_id" => '...',
    "player_name" => '...',
    "player_country" => '...',
    "player_image" => '...',
    "player_league" => '...'
);

$json = json_encode($myCallbackArray);

$json is actually the following:

[{"player_id":"...","player_name":"...","player_country":"...","player_image":"...","player_league":"..."}]

which is valid JSON and you could easily use it in javascript.

Krasimir
  • 13,306
  • 3
  • 40
  • 55
0

I think your accessing the data wrong in your success function, the data comes back as an array. Here is an example:

var request = $.ajax({
    type: "POST",
    url: url,
    dataType: 'json',
    data: {user_id: id},
    success: function(data){

        var myval = data["returned_val"];

        alert(myval);

    },
    error:function() {
        //FAILURE
    }
});
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338