0

I'm creating a RPG. When the user logs in, I want to load their data from three tables:

  • quest_items stores all data related to an item:

  • join_questitems stores association between player ID and the items that player has:

  • userstats stores user_id, current map, current quest, x y location, etc

Since I want to load all this data on runtime, I figured I'd just put the two queries in the same function, store results in separate arrays, then push those two arrays into allData array. Then encode allData and pass it back to JS.

The problem here is accessing the data in the $.(each loop:


PHP:

$allData = array();
$itemsArray = array();
$statsArray = array();

$qry = 
    'SELECT qi.* 
    FROM quest_items qi
    LEFT JOIN join_questitems jqi ON (qi.item_id = jqi.item_id)
    WHERE jqi.user_id = "' . $playerID . '"';

$result = $mysqli->query($qry) or die(mysqli_error($mysqli));

while ($row = $result->fetch_assoc()) {
    $itemsArray[] = $row;
}   

$qry = 
    'SELECT us.* 
    FROM userstats us
    WHERE us.id_user_fk = "' . $playerID . '"';

$result = $mysqli->query($qry) or die(mysqli_error($mysqli));

while ($row = $result->fetch_assoc()) {
    $statsArray[] = $row;
}   

array_push($allData, $itemsArray, $statsArray);

echo json_encode($allData);

JavaScript snippet:

I'm able to access the first array ($itemsArray) within $allData with v[0].item_name from quest_items table, which gives: rice, test/path.png as expected.

But if I try to access the second array v[1].username from userstats table, it gives undefined.

$.getJSON("phpscripts.php", {
    "_player" : Player,
    "_playerID" : UserID
},
function(returned_data) {                       
    $.each(returned_data, function (key, value) {       
        $(".questItems").append("<br/>" + value[1].username + ", " + value[0].item_name);   
    });
  • value[1].username = undefined
  • value[0].item_name = works

This is curious because the chrome debugger response shows that both arrays are being added and returned inside the container array:

enter image description here

Why is this? Why can't I access array 2 with value[1].username?


EDIT:

$.each(returned_data, function (key, value) {               
    console.log(value[1]);  
});

Gives:

Object {item_id: "2", item_name: "meat", item_chinese: "rou", ...   (index):29
undefined (index):29

Which is a result set from value[0]. Maybe what's happening is either

  • since value[0] has more result rows than does value[1], maybe it's looping through again for value[1] even though it is passed its array size. Would that break it?

  • value[1] is actually accessing the second array value from the first items array... and not pointing to the userstats array?


EDIT 2:

console.log(returned_data) right after function(returned_data) gives:

[Array[2], Array[1]]
0: Array[2]
1: Array[1]
length: 2
__proto__: Array[0]

console.log(value); right after $.each(returned_data, function (key, value) { gives:

[Object, Object]
0: Object
1: Object
length: 2
__proto__: Array[0]

[Object]
0: Object
length: 1
__proto__: Array[0]

Expanded results of console.log(returned_data); after function(returned_data):

enter image description here


EDIT 3: It works if I manually access the array: returned_data[1][0]["username"]

function(returned_data) {
    console.log(returned_data[1][0]["username"]); //DAN
    console.log(returned_data[0][0]["item_name"]); //rice
    console.log(returned_data[0][1]["item_name"]); //meat

But what if I have a lot of data rows to return for item_name. I need a loop to get all data.

6
  • What do you get if you console.log(v[1]);? Is v[1].item_name meat? Commented May 16, 2014 at 1:42
  • Where are you setting v? It seems it's already diving into the 0 of the container. Use better variable names than v. :) Commented May 16, 2014 at 1:48
  • @HamzaKubba see update. V is the value I'm setting in $.each. and yes, v[1].item_name is meat Commented May 16, 2014 at 1:51
  • console.log(returned_data) right after function(returned_data) {, and console.log(value) right after $.each(returned_data, function (key, value) { Commented May 16, 2014 at 1:53
  • And although value is better than v, something descriptive like userData or userRawData would bring more clarity to the code (not required by any means). Commented May 16, 2014 at 1:55

1 Answer 1

1

The first time the function passed to $.each() runs, value is the 0 subcontainer which has 0 and 1. The second time the function runs, value is the 1 subcontainer already.

If returned_data[0] and returned_data[1] are different and you want to do different things with them, don't use $.each(), just use returned_data[0] and returned_data[1].

So:

$.each(returned_data[0], function (key, value) {       
    $(".questItems").append("<br/>" + returned_data[1][0].username + ", " + value[0].item_name);   
});
Sign up to request clarification or add additional context in comments.

8 Comments

Okay I realize this. How can I accomplish getting the results from two arrays passed from a containing PHP array with $.each loop?
Updated with that... don't use $.each(), use returned_data[0] and returned_data[1].
Or maybe returned_data[0][0] and returned_data[1][0]. Again, it's better to give names rather than 0 and 1, so you don't get confusing data.
those are array indexes - not variable names
They would still work, but you can do returned_data[0][0+""] and returned_data[1][0+""] if you want to be a stickler for detail. That said, JS recognizes that returned_data[0] and returned_data[1] are objects, and converts the last 0 to a string before looking it up.
|

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.