Need help with coding a PHP comment system?

punkstjimmy

New member
I have my comments in one table, I have my posts in another. Each post has an ID, and each comment has a postid which is the ID of the post the comment goes with

I grabbing the posts from the database and putting them in an array. I am then displaying the posts on a page using a foreach loop. So far so good.

Somehow I now need to pull the comments for each post from the database and have them display under the post which they belong to. The trouble i'm having is that when I pull them out, they display under every post rather than just the post that they belong to.

I'm not asking for anyone to code this for me, but can someone just explain the process of how I would get the comments by their postid and have them display under the correct post.

If you need more info, just ask.
Thanks
That is what I have tried. The problem is, instead of starting a new array for the next post in the loop it just adds the comments to the first array and displays them all.

So the under the first post it displays all the comments for the first post.

Under the 2nd post it displays all the comments for the first post, with the comments for the 2nd post added on.

And so on... how do I get it to "clear" the comments array after it loops through each post?
Ok I have it now. I used unset to clear the array after each loop, so the next loop only contained the comments from the current post and not all the ones before it.
 
Back
Top