简体   繁体   English

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

[英]Retrieve data from PHP file using $.getJSON

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: get.php:

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

main.php: 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 | 我的评论表有以下4列: id | photo_id | photo_id | comment | 评论| date 日期

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: 在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. 另外,我有两个需要在同一个PHP文件中回显的json数组。 ie echo json_encode(array('img1'=>$img1link)) and echo json_encode($comments); echo json_encode(array('img1'=>$img1link))echo json_encode($comments); need to be echoed within the same PHP file, but it made the code stop working altogether. 需要在同一个PHP文件中回显,但它使代码完全停止工作。

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));
[{"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. 使用此JSON, data是一个数组,您应该将其作为数组进行管理。 You can loop through it using simple loops ( for , while ...) or using new functional methods like forEach , map , filter .... 您可以使用简单循环( forwhile ...)或使用新的函数方法(如forEachmapfilter ....)循环它。

Please try with this example: 请尝试使用此示例:

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

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM