Display all comments related to their posts

Okay so I need a little help here. I have an issue where I’m getting a comments-display.php script via Ajax but the right content is not showing the way I want it to. So what I mean is I am able to comment but the comment shows up for every other post instead of it being for just one post. I have a query to get all the comments and display them for a specific post but it doesn’t seem to be working.
SELECT * FROM comments LEFT OUTER JOIN feed ON comments.status_id = feed.id ORDER BY comments.id DESC

I’ve also tried

SELECT comments.status_id, comments.comment, comments.p_id, feed.id, feed.p_id FROM comments INNER JOIN feed ON comments.status_id = feed.id ORDER BY comments.id DESC
Yes it’s getting the comment but it’s not displaying it for that particular post with the same id, rather it’s displaying it for every post on the page. To explain my query I have it so that comments.status_id is set equal to feed.id so if status_id was 1150 feed.id would also be 1150 and it would get all the comments for id 1150.

On the face of it, if you only want the comments for a specific post, won’t you need a WHERE clause to restrict the query to return only comments for a specific post ID? Hard to tell without knowing the database layout in a bit more detail and/or seeing the code. And is it correct to order by comments.id rather than comments.status_id?

What would I need then? Would I use an if statement instead and getting the two fields to match?

comments.id is the auto increment value, comments.status_id is the feed.id it’s getting, so if feed.id is 1150 comments.status_id would be 1150 and comments.id would like 300 or whatever so that way if there are more comments for id 1150 it’s in order.

Okay so scrap this question, I got it to work but now I need help with something else…What I did to get it to work instead was just, to get the comments when the form was posted using Ajax :stuck_out_tongue: so now it instantly gets the comment when posted but it does not appear for everyone. Example below.

Example: Lets say I’m logged in as user1 and I’m posting a comment on user2’s post. It posts the comment and it shows automatically for user1 but not for user2, user2 has to refresh the page in order to see the comment that was posted. How do I make it so that it displays for everyone when it’s posted. This is what I"m using for both posting and getting the comment, yes I realize there may be no reason for 2 ajax calls but it works so I’m leaving it :stuck_out_tongue:

<script type='text/javascript'>
	$("#comment_form-<?php echo $row_feed_modal['id']; ?>").submit(function(e) {
		var url = "scripts/comment-status.php"; // the script where you handle the form input.
		$.ajax({
			type: "POST",	
			url: url,
			data: $("#comment_form-<?php echo $row_feed_modal['id']; ?>").serialize(), // serializes the form's elements.
			success: function(data) {
				$('#comment').val('');
			}
		});
		e.preventDefault(); // avoid to execute the actual submit of the form.
		$.ajax({
			type: "POST",
			url: "scripts/comments-display.php",
			data: $("#comment_form-<?php echo $row_feed_modal['id']; ?>").serialize(), // serializes the form's elements.
			success: function(data) {
				$("#comments_display1-<?php echo $row_feed_modal['id']; ?>").hide();
				$("#hide_comments_area-<?php echo $row_comments_modal['id']; ?>").hide();
				$("#show_comments_area-<?php echo $row_comments_modal['id']; ?>").show();
				$("#show_comments_area-<?php echo $row_comments_modal['id']; ?>").html(data);
			},
			error: function(){}
		});
		e.preventDefault(); // avoid to execute the actual submit of the form.
	});
</script>

notice that both url’s are different, one for posting and one for displaying

Won’t you need to have every user periodically poll the database to see if any new comments have been posted since they last retrieved them? In your example, user1 sees the new post because that’s part of the success() function on return from the php script, but something needs to trigger anyone who didn’t make the post into retrieving the new post(s). I think a common way is to use a timer to check for new comments, though I’m no Ajax expert and it might get more response outside of the specific PHP forum.

Is there any way to move this post to JavaScript or somewhere or add new tags so others can chime in?

What you’ve suggested was what I tried and my problem when I first posted the first question, but what was happening was it was getting the comments but not for the correct post. Like, yea I was able to call the script every 10 - 15 seconds and show to comment that was posted but instead of getting the comment for that exact post, it displayed any and every comment in the database for every post.

So an example would be I have 3 comments for ID 21 and 3 comments for ID 22, the 3 comments for ID 21 will show up for ID 22 as well as any other ID and vice versa if I call the script.

What my query is/was doing is this: SELECT * FROM comments WHERE status_id = '$_POST[status_id]' ORDER BY sqlid DESC sqlid being the auto increment value. So when I call the script to run that query it has no clue what $POST[status_id] is because it was never posted, so now I have 2 scripts, one posting the comment and one retrieving the comment. The one posting the comment works but when I try to retrieve the comment at the same time as when the comment is posted it gives me an Unidentified index status_id on line whatever, so what I then tried to do was create another ajax script and pass that status_id from another form as the script is being called, never worked :confused: I probably just made it more confusing than it sounds…

But this is the problem I’m having lol the script works but I cant pass _$_POST[status_id]/it doesn’t know what status_id is

When you display the post and its comments initially, if you do that using PHP or something to generate the page code, you can stick the value of status_id into a html field somewhere (which might be hidden if you don’t want the user to see it), then retrieve it as part of your JavaScript when you call the refresh code, and pass it to the refresh code as part of the data string.

So what I ended up doing was just posting to the database again (using ajax) but this time without really posting the form a second time. It just posted an “empty” form but still passed the status_id. This worked like a charm though :slight_smile:

<script>
setInterval(function() {
	$.ajax({
		type: "POST",
		url: "scripts/comments-display.php",
		data: $("#comm_display_from-<?php echo $row_feed_modal['lid']; ?>").serialize(), // serializes the form's elements.
		success: function(data) {
			$("#comments_display1-<?php echo $row_feed_modal['id']; ?>").hide();
			$("#hide_comments_area-<?php echo $row_comments_modal[lid']; ?>").hide();
			$("#show_comments_area-<?php echo $row_comments_modal['id']; ?>").show();
			$("#show_comments_area-<?php echo $row_comments_modal['id']; ?>").html(data);
		}
	});
}, 5000);
</script>
1 Like

Use the “flag” icon on the post, choose the "something else"option and let moderators know where you want it moved. (I have moved it to JS.)

I did not know I could do that, thank you for moving it :slight_smile: and I will do that next time. It’d be cool to have a PHP and JavaScript section though

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.