Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JSON上循环所有数据_Javascript_Php_Json_While Loop - Fatal编程技术网

Javascript 如何在JSON上循环所有数据

Javascript 如何在JSON上循环所有数据,javascript,php,json,while-loop,Javascript,Php,Json,While Loop,您好,我尝试循环我的json\u encode(),以获取基于我的查询的所有相关数据 脚本工作正常,但循环部分不正常 以下是我目前掌握的情况: $con3 = new PDO("mysql:host=". db_host .";dbname=db", db_username , db_password); $con3->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $con4 = new PDO("mysql:hos

您好,我尝试循环我的
json\u encode()
,以获取基于我的查询的所有相关数据 脚本工作正常,但循环部分不正常

以下是我目前掌握的情况:

$con3 = new PDO("mysql:host=". db_host .";dbname=db", db_username , db_password);
$con3->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$con4 = new PDO("mysql:host=". db_host .";dbname=chat_db", db_username , db_password);
$con4->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql5 = "SELECT * FROM users WHERE id = :rid LIMIT 1";
$stmt6=$con4->prepare($sql5);
$stmt6->bindValue( 'rid',$_GET['rid'], PDO::PARAM_STR);
$stmt6->execute();
    foreach($stmt6->fetchAll()as $res)
        {
            $usern = $res['username'];
            $user_lvl = $res['ulvl'];
        }

$comb = $usern . $_GET['name'];


  $sql6="SELECT msgid FROM thread WHERE combination1=:msgids OR combination2=:submsgids";
  $msg_id = $con4->prepare($sql6);
  $msg_id->bindParam(':msgids', $comb, PDO::PARAM_STR);
  $msg_id->bindParam(':submsgids', $comb, PDO::PARAM_STR);
  $msg_id->execute();
  $msgd = $msg_id->fetchColumn();
  $tbpre = $msgd;
    $sql7 = "SELECT   message_content, username , message_time, recipient FROM ".$tbpre."chat_conversation WHERE msgid=:chat";

    $stmt7=$con3->prepare($sql7);
    $stmt7->bindValue( 'chat', $msgd, PDO::PARAM_STR);
    $stmt7->execute();



  $message_query = $stmt7;


if(count($message_query) > 0) {
    while($message_array = $stmt7->fetchAll(PDO::FETCH_ASSOC)) {
        echo json_encode($message_array);
    }
}
它只从我的数据库返回一个数据

这是我的javascript,用于从php端检索:

        function AjaxRetrieve()
        {
          var rid = document.getElementById('trg').value,
    data = {chat: uid, rid: rid, name: user};

$.get('includes/getChat.php', data, function (result) {
    var res = $([]);

    $.each(result[0], function(key, value) {
        res = res.add($('<div />', {text : value}));
    });

    $("#clog").html(res);

}, 'json');
        }
函数AjaxRetrieve()
{
var rid=document.getElementById('trg')。值,
data={chat:uid,rid:rid,name:user};
$.get('includes/getChat.php',数据,函数(结果){
var res=$([]);
$.each(结果[0],函数(键,值){
res=res.add($('',{text:value}));
});
$(“#clog”).html(res);
}“json”);
}

每次循环迭代都会覆盖$json\u字符串

您要做的是将DB数据保存到数组中,然后进行编码

$array = array();
if(count($message_query) > 0) {
    while($message_array = $stmt7->fetchAll(PDO::FETCH_ASSOC)) {
        $array[] = $message_array;   
    }
}
$json_string = json_encode($array);
echo $json_string;
在AJAX成功处理程序函数中,似乎没有正确地迭代数组

请注意,您仅对返回结果集的第一个数组元素调用
$。each()

$.each(result[0], function(key, value) {
    res = res.add($('<div />', {text : value}));
});
$。每个(结果[0],函数(键,值){
res=res.add($('',{text:value}));
});
我认为您应该像这样迭代整个结果集:

$.each(result, function(rowKey, row) {
    // row is single row of result set from database
    // rowKey is numerical index of the row in the result set
    // rowKey is probably not useful here
    // You can just append new div to #clog with
    // whatever content from row which you desire
    // for example, this would insert message_content value
    // in child div to #clog
    $("#clog").append('<div>' + row.message_content + '</div>');
});
$。每个(结果、函数(行键、行){
//行是数据库中结果集的单行
//rowKey是结果集中行的数字索引
//rowKey在这里可能没有用处
//您只需将新div附加到#clog with
//无论您想要哪一行的内容
//例如,这将插入消息内容值
//在儿童驾驶室里
$(“#clog”).append(“”+row.message_content+“”);
});

每次循环迭代都会覆盖$json\u字符串

您要做的是将DB数据保存到数组中,然后进行编码

$array = array();
if(count($message_query) > 0) {
    while($message_array = $stmt7->fetchAll(PDO::FETCH_ASSOC)) {
        $array[] = $message_array;   
    }
}
$json_string = json_encode($array);
echo $json_string;
在AJAX成功处理程序函数中,似乎没有正确地迭代数组

请注意,您仅对返回结果集的第一个数组元素调用
$。each()

$.each(result[0], function(key, value) {
    res = res.add($('<div />', {text : value}));
});
$。每个(结果[0],函数(键,值){
res=res.add($('',{text:value}));
});
我认为您应该像这样迭代整个结果集:

$.each(result, function(rowKey, row) {
    // row is single row of result set from database
    // rowKey is numerical index of the row in the result set
    // rowKey is probably not useful here
    // You can just append new div to #clog with
    // whatever content from row which you desire
    // for example, this would insert message_content value
    // in child div to #clog
    $("#clog").append('<div>' + row.message_content + '</div>');
});
$。每个(结果、函数(行键、行){
//行是数据库中结果集的单行
//rowKey是结果集中行的数字索引
//rowKey在这里可能没有用处
//您只需将新div附加到#clog with
//无论您想要哪一行的内容
//例如,这将插入消息内容值
//在儿童驾驶室里
$(“#clog”).append(“”+row.message_content+“”);
});
请使用计数行:

$json = array();

if($message_query->rowCount() > 0) {
    while($message_array = $stmt7->fetchAll(PDO::FETCH_ASSOC)) {
        $json[] = $message_array;
    }
}
echo json_encode($json);
您应该使用数组来存储消息数组,这样您就不会反复替换同一个变量。

请使用来计算行数:

$json = array();

if($message_query->rowCount() > 0) {
    while($message_array = $stmt7->fetchAll(PDO::FETCH_ASSOC)) {
        $json[] = $message_array;
    }
}
echo json_encode($json);
您应该使用一个数组来存储消息数组,这样您就不会一次又一次地替换同一个变量。

试试这个

$i=0;
$fet=mysql_query('select * from tbl1 WHERE tbl1.id='20');
while($row=mysql_fetch_assoc($fet,MYSQL_ASSOC)){ 
$json[$i]['date']=$row['time'];
$i++;
} 
echo json_encode($json);
试试这个

$i=0;
$fet=mysql_query('select * from tbl1 WHERE tbl1.id='20');
while($row=mysql_fetch_assoc($fet,MYSQL_ASSOC)){ 
$json[$i]['date']=$row['time'];
$i++;
} 
echo json_encode($json);

&该数据必须是最后一个?$json\u string+=json\u encode($message\u array)@DeepakMane这不是正确的方法是$json_string_array[]=($message_array);最后一行是echo json_encode($json_string_array)@DeepakMane您提供的内容没有显示任何内容。。为什么会这样?&数据必须是最后一个?$json\u string+=json\u encode($message\u数组)@DeepakMane这不是正确的方法是$json_string_array[]=($message_array);最后一行是echo json_encode($json_string_array)@DeepakMane您提供的内容没有显示任何内容。。这是为什么?嗨,我试过这个代码,输出是
[object object][object object][object object][object object][object object][object object][object object][object object][object object][object object][object object][object object][object object object object object][object object object object object object][object object object object?这看起来像是您尝试
回送对象时会得到的结果,而不是您对数组进行JSON编码然后回送JSON字符串时会得到的结果。我查看了您脚本的响应,它以数组格式显示了一组来自数据库的所有数据。。但是为什么它在我的页面上显示
[object object]
=(@user256009它看起来像是在AJAX成功处理程序中处理
result
的地方,您只是在返回的第一个数组项上调用
$.each()
我想你会想迭代整个结果集。我会用一些额外的注释更新答案。你的右主人@Mike Brant它给了我所有的消息内容……但这是否意味着我必须添加更多的
$(“#clog”)。追加('+row.message_content+'');
让我根据自己的需要显示其他数据?例如,我还需要从json结果中添加用户名和时间/日期。我还需要添加其中的两个吗?您好,我已经尝试过这段代码,输出是
[object object object][object object][object object][object object][object object][object object object object object][object object object object object object][对象][对象]
知道我遗漏了什么吗?@user256009你能展示一下你的用法吗?这看起来像是你尝试
回送对象时会得到的结果,而不是你对数组进行JSON编码然后回送JSON字符串时会得到的结果。我已经查看了你脚本的响应,它在数组f上显示了一组我数据库中的所有数据ormat..但是为什么它在我的页面上显示
[object object]
=(@user256009它看起来像是在处理AJAX成功处理程序中的
result
,您只在返回的第一个数组项上调用
$.each()
我想你会想迭代整个结果集。我会用一些额外的注释更新答案。你的右主人@Mike Brant它给了我所有的消息内容……但这是否意味着我必须发布广告