回声所有json_en codeD行 [英] Echo all json_encoded rows

查看:201
本文介绍了回声所有json_en codeD行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过我的数据库,并试图输出循环中的所有行具有匹配的连接表。

I'm trying to loop through my database and output all rows with a match to the joined table.

我有以下两个表:

quest_items 相关项目存储所有数据:

quest_items stores all data related to an item:

join_questitems 玩家ID和玩家有项目之间的关联专卖店:

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

JS:传递所有必要的信息,以查询表...

JS: pass in all the necessary info to query the table...

$.getJSON("phpscripts.php", {
    "_player" : Player,
    "_playerID" : UserID
},
function(returned_data) {
    var item_name = returned_data.item_name;
    item_image = returned_data.item_image;

    $(".questItems").html(item_name + ", " + item_image);       
   }
);  

PHP:

$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()) {
    $myrow = json_encode($row);
    array_push($statsArray, $myrow);
}   
$k = 0;
while ($k < count($statsArray)) {
    echo $statsArray[$k];
    $k++;
}

但如果我只是做一排,我得到的输出,但只有一行。我需要两行:

But if I just do one row, I get output, but only for one row. I need both rows:

while ($row = $result->fetch_assoc()) {
    echo json_encode($row);
    exit;
}   

$(。questItems)HTML(ITEM_NAME +,+ item_image)给出:大米,测试/ path.png

为什么我不能遍历数组PHP充分json_en codeD行和输出他们?我一定要设置 ITEM_NAME item_image 数组和遍历数组?

Why can't I loop through the PHP array full of json_encoded rows and output them? Do I have to set item_name and item_image as arrays and loop through the arrays?

编辑:


  • $(。questItems)。追加(ITEM_NAME +,+ item_image)无法正常工作或

从调试网络输出显示行正在输出,但排在第一位,我的UTF-8字符,风扇,被在客户端输出乱码(如你看到的)。这是奇怪的,因为我的 phpscripts.php 文件的顶部,我把 mysqli_set_charset($ mysqli的UTF8);

network output from debugger shows rows are being output, but in the first row, my UTF-8 character, fàn, is being garbled in the client output (as you can see). This is curious, as at the top of my phpscripts.php file, I put mysqli_set_charset($mysqli, "utf8");

推荐答案

您不应该带code的每一行分别。把数组中的所有结果,然后调用 json_en code 当你完成了阵列上:

You shouldn't encode each row separately. Put all the results in an array, and then call json_encode on that array when you're done:

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

然后在Javascript中,你需要循环返回数组过:

Then in Javascript you need to loop over the returned array:

$.getJSON("phpscripts.php", {
    "_player" : Player,
    "_playerID" : UserID
},
    function(returned_data) {
        $.each(returned_data, function(i, e) {
            var item_name = e.item_name;
            var item_image = e.item_image;

            $(".questItems").append(item_name + ", " + item_image + "<br/>");       
        });
    };  
);

这篇关于回声所有json_en codeD行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆