dongxi1680 2014-05-16 01:30
浏览 19
已采纳

将多个查询结果行加载到数组中

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.

  • 写回答

1条回答 默认 最新

  • doubeng9407 2014-05-16 01:58
    关注

    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);   
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路