如何在for循环中获取json数据for

时间:2017-11-09 08:53:25

标签: php jquery arrays ajax

php代码:

$query = mysqli_query($con,"INSERT INTO tlb_comments(user_id,comments) values ('$vw_id','$name')");
$comments = mysqli_query($con,"SELECT * FROM tlb_comments");
$avatar   = mysqli_query($con,"SELECT * FROM tlb_avatar WHERE `vw_id`='".$vw_id."'");
$avatar_select  =  mysqli_fetch_all($avatar);
$comment_select =  mysqli_fetch_all($comments);
array_push($avatar_select,$comment_select);
echo json_encode($avatar_select);

Jquery Ajax:

$(".comment").click(function() {
    var id = $(this).data("id");
    var name = $("#username_" + id).val();
    if (name == '') {
        alert("Please Fill All Fields");
    } else {

        $.ajax({
            type: "POST",
            url: "comments",
            data: {
                username: name
            },
            success: function(html) {

                $("#cmt_output").html(html);
                console.log(html);
            }
        });
    }
});

Json Array:

[
   [
      "11",
      "6",
      "1509947417_User_Avatar_2.png"
   ],
   [
      [
         "113",
         "0",
         "6",
         "all of us",
         "0"
      ],
      [
         "114",
         "0",
         "6",
         "all of us",
         "0"
      ],
      [
         "115",
         "0",
         "6",
         "welcome....",
         "0"
      ]
   ]
]

1 个答案:

答案 0 :(得分:0)

我认为你必须从php返回asssociative数组,这样你就可以用每个数组元素的键轻松读取json数组......