3

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


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

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

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

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):

[{"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?

$.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.

2
  • 1
    If you want to display the comments you need to loop over the array. You can use for loop or forEach function. Commented May 13, 2015 at 15:00
  • I think you mean main.js:. Commented Feb 15, 2017 at 17:09

3 Answers 3

3

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

$.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.

echo json_encode(array('img' => $img1link, 'comments' => $comments));
Sign up to request clarification or add additional context in comments.

2 Comments

For some reason I can't get this working. I put your code in a completely new file, added a <div></div> put the two JSONs in the same array etc.
@frosty If you put both JSONs in array as in second code, you will need to use data.comments.forEach
0
[{"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....

Please try with this example:

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

Comments

0

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

2 Comments

data is an array. data.id is undefined.
btw commentsArr will be the same as data... don't you think?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.