main.php jQuery code:
$.getJSON('posts.php',function(data){
data.posts.forEach(function(post){
// set variables and append divs to document
})
data.comments.forEach(function(post){
// set variables and append divs to document
})
})
(Old - Works with current jQuery code)
Example object containing 2 posts and 3 comments. Post with id: 5
has 1 comment and post id: 2
has 2 comments.
// the two posts ID: 5 and 2
{"posts":[{
"id":"5",
"image":"link.jpg",
"submitter":"4322309",
"views":"3"
},
{
"id":"2",
"image":"link.jpg",
"submitter":"4322309",
"views":"10"
}],
// now each comment tied to the posts
"comments":[
{
"id":"1",
"submitter":"submitter",
"time":"2435657",
"comment":"comment",
"score":"10",
"postid":"2"
},
{
"id":"2",
"submitter":"submitter",
"time":"2435657",
"comment":"comment",
"score":"10",
"postid":"2"
},
{
"id":"3",
"submitter":"submitter",
"time":"2435657",
"comment":"comment",
"score":"10",
"postid":"5"
}]}
(NEW - Does not work with current jQuery code)
Example object containing 2 posts and 3 comments. Post with id: 5
has 1 comment and post id: 2
has 2 comments.
// the two posts ID: 5 and 2
{
"posts":{
"5": {
"id":"5",
"image":"link.jpg",
"submitter":"4322309",
"views":"3"
},
"2": {
"id":"2",
"image":"link.jpg",
"submitter":"4322309",
"views":"5"
}
},
// now each comment tied to the posts
"comments":{
"2": [{
"id":"1",
"submitter":"submitter",
"time":"2435657",
"comment":"comment",
"score":"10",
"postid":"2"
},
{
"id":"2",
"submitter":"submitter",
"time":"2435657",
"comment":"comment",
"score":"10",
"postid":"2"
}
],
"5": [{
"id":"3",
"submitter":"submitter",
"time":"2435657",
"comment":"comment",
"score":"10",
"postid":"5"
}]
}
}
I'm not sure how to use this JSON object in this new scenario.
Basically how do I loop through this new one?