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

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流