使用$ .getJSON从PHP文件中检索数据

时间:2022-09-25 23:16:25

I'm trying to set up a comments system on photos.

我正在尝试在照片上建立评论系统。


I understand how to use $.getJSON when the array is like this:

我理解当数组如下时如何使用$ .getJSON:

get.php:

$var = 5;
echo json_encode(array('var'=>$var));

main.php:

$.getJSON("get.php",function(data){
    number = data.var; // number = 5
});

But I have a more complex thing.

但我有一个更复杂的事情。


My comments table has these 4 columns: id | photo_id | comment | date

我的评论表有以下4列:id | photo_id |评论|日期

For example let's say we're trying to retrieve the comment data from the photo with

例如,假设我们正试图从照片中检索评论数据

photo_id == 1.

photo_id == 1。

We don't know how many comments there might be.

我们不知道可能有多少评论。

In getcomments.php:

$photoid = 1;

$comment = mysqli_query($conn,"SELECT * FROM comments WHERE photo_id='$photoid'");

while($commentrow = $comment->fetch_assoc()) {
    $comments[] = $commentrow;
}

Then you encode it:

然后你编码它:

echo json_encode($comments);

Which prints something like this (the photo has 2 comments):

打印出这样的东西(照片有2条评论):

[{"id":"1","photo_id":"1","comment":"text","date":"23858556"},{"id":"2","photo_id":"1","comment":"text","date":"23858561"}]

How do I declare variables for the comments array?

如何为comments数组声明变量?

$.getJSON("getcomments.php",function(data){
    // how do I declare variables for the comments array, especially since you don't know how many there might be?
});

Additionally, I have two json arrays that need to be echoed within the same PHP file. i.e. echo json_encode(array('img1'=>$img1link)) and echo json_encode($comments); need to be echoed within the same PHP file, but it made the code stop working altogether.

另外,我有两个需要在同一个PHP文件中回显的json数组。即echo json_encode(array('img1'=> $ img1link))和echo json_encode($ comments);需要在同一个PHP文件中回显,但它使代码完全停止工作。

3 个解决方案

#1


If you want to display the comments you need to loop over the array. You can use for loop or forEach function.

如果要显示循环数组所需的注释。您可以使用for循环或forEach函数。

$.getJSON("getcomments.php",function(data){
    data.forEach(function(comment) {
        $('div').append('<span>' + comment.comment + '</span>');
    });
});

To display two JSONs you need to combine them into one JSON object.

要显示两个JSON,您需要将它们组合成一个JSON对象。

echo json_encode(array('img' => $img1link, 'comments' => $comments));

#2


[{"id":"1","photo_id":"1","comment":"text","date":"23858556"},{"id":"2","photo_id":"1","comment":"text","date":"23858561"}]

Using this JSON, data is an array and you should manage it as an array. You can loop through it using simple loops (for, while...) or using new functional methods like forEach, map, filter....

使用此JSON,数据是一个数组,您应该将其作为数组进行管理。您可以使用简单循环(for,while ...)或使用新的函数方法(如forEach,map,filter ....)循环它。

Please try with this example:

请尝试使用此示例:

$.getJSON("getcomments.php",function(data){
    data.forEach(function(item, index, all) {
        console.log(item.comment);
    });
});

#3


Declare an object, and push it to the array.

声明一个对象,并将其推送到数组。

var commentsArr = [];

for (var i = 0; i < data.length; i++) {
    var objToPush = {
        id: data.id,
        comment: data.comment,
        date: data.date
    }

    commentsArr.push(objToPush);
}

#1


If you want to display the comments you need to loop over the array. You can use for loop or forEach function.

如果要显示循环数组所需的注释。您可以使用for循环或forEach函数。

$.getJSON("getcomments.php",function(data){
    data.forEach(function(comment) {
        $('div').append('<span>' + comment.comment + '</span>');
    });
});

To display two JSONs you need to combine them into one JSON object.

要显示两个JSON,您需要将它们组合成一个JSON对象。

echo json_encode(array('img' => $img1link, 'comments' => $comments));

#2


[{"id":"1","photo_id":"1","comment":"text","date":"23858556"},{"id":"2","photo_id":"1","comment":"text","date":"23858561"}]

Using this JSON, data is an array and you should manage it as an array. You can loop through it using simple loops (for, while...) or using new functional methods like forEach, map, filter....

使用此JSON,数据是一个数组,您应该将其作为数组进行管理。您可以使用简单循环(for,while ...)或使用新的函数方法(如forEach,map,filter ....)循环它。

Please try with this example:

请尝试使用此示例:

$.getJSON("getcomments.php",function(data){
    data.forEach(function(item, index, all) {
        console.log(item.comment);
    });
});

#3


Declare an object, and push it to the array.

声明一个对象,并将其推送到数组。

var commentsArr = [];

for (var i = 0; i < data.length; i++) {
    var objToPush = {
        id: data.id,
        comment: data.comment,
        date: data.date
    }

    commentsArr.push(objToPush);
}