Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.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_Ajax_Json - Fatal编程技术网

Javascript 单独显示json_编码结果

Javascript 单独显示json_编码结果,javascript,php,ajax,json,Javascript,Php,Ajax,Json,您好,我能够显示json数据(对话结果列表),但我想显示它们,就像显示数据库记录一样,如下所示: foreach(res->fetchAll(PDO::FETCH_ASSOC) as result): $username = $result['username']; $message = $result['message']; endforeach; 除了使用json\u encode(),还有类似的过程吗 这是我的php脚本 $sql6="SELECT m

您好,我能够显示json数据(对话结果列表),但我想显示它们,就像显示数据库记录一样,如下所示:

   foreach(res->fetchAll(PDO::FETCH_ASSOC) as result):
    $username = $result['username'];
    $message = $result['message'];
    endforeach;
除了使用
json\u encode()
,还有类似的过程吗

这是我的php脚本

$sql6="SELECT msgid FROM thread WHERE combination1=:msgids OR combination2=:submsgids LIMIT 1";
        $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 * FROM ".$tbpre."chat_conversation WHERE msgid=:chat";

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

        echo json_encode($rows);
这个php脚本从下面我的ajax脚本中获取数据

        function AjaxRetrieve()
            {
              var rid = document.getElementById('trg').value,
            data = {chat: uid, rid: rid, name: user};
 $.ajax({
      url: "includes/getChat.php",
      type: "GET",
      data: data,
  dataType: 'json',
 success: function(result){
        var container = $("#clog");

        $.each(result, function(i, message) {
            $.each(message, function(key, value) {
                container.append($('<p />').html(key + ':' + value));
            });
        });
   }
});
函数AjaxRetrieve()
{
var rid=document.getElementById('trg')。值,
data={chat:uid,rid:rid,name:user};
$.ajax({
url:“includes/getChat.php”,
键入:“获取”,
数据:数据,
数据类型:“json”,
成功:功能(结果){
变量容器=$(“#阻塞”);
$。每个(结果、函数(i、消息){
$。每个(消息、函数(键、值){
container.append($('

').html(key+':'+value)); }); }); } });


php和javascript工作正常,JSON正在显示中。但是我正在尝试找出是否有一种方法,我可以只显示用户名和消息,以及我如何使用我上面提到的
foreach
语句…

我可能会误解,但您应该能够通过添加一个con来过滤显示的内容如果

$.each(result, function(i, message) {
    $.each(message, function(key, value) {
        if (key == "username" || key == "message")
            container.append($('<p />').html(key + ':' + value));
    });
});
$。每个(结果、函数(i、消息){
$。每个(消息、函数(键、值){
如果(键==“用户名”| |键==“消息”)
container.append($('

').html(key+':'+value)); }); });


Hi..我尝试了你的代码,我只能显示用户名而不能显示消息。如果消息显示为用户名,我应该用什么替换
|
|
在javascript上也称为
,顺便说一句?我对javascript很感兴趣,如果这有点烦人的话。。