Jump to content

pagination for comments section


ryanmetzler3

Recommended Posts

I got this section where people post comments. Here is a while loop that pulls all the comments and passes them to the function getComments. Then I will also show the function "getComments" that displays them.

//while loop to get comments
<?php
$q = "SELECT * FROM threaded_comments WHERE parent_id = 0 ORDER BY id DESC";
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)):
	getComments($row);
endwhile;
?>

// function to display them
<?php
function getComments($row) {
	echo "<li class='comment'>";
	echo "<div class='aut'>".$row['author']."</div>";
	echo "<div class='comment-body'>".$row['comment']."</div>";
	echo "<div class='timestamp'>".$row['created_at']."</div>";
	echo "<a href='#comment_form' class='reply' id='".$row['id']."'>Reply</a>";
	$q = "SELECT * FROM threaded_comments WHERE parent_id = ".$row['id']."";
	$r = mysql_query($q);
	if(mysql_num_rows($r)>0)
		{
		echo "<ul>";
		while($row = mysql_fetch_assoc($r)) {
			getComments($row);
		}
		echo "</ul>";
		}
	echo "</li>";
}
?>

The second if-statement in the function to retrieve replies to comments.

 

How could I go about doing pagination so maybe 15 original comments on a page display (I am not worried about replies)? Then have the option to page over and view more. I was thinking of using the LIMIT attribute in the first query, but then I would not know the total number of comments. Sorry for such a dumb question, but I am new to programming

Link to comment
https://forums.phpfreaks.com/topic/284885-pagination-for-comments-section/
Share on other sites

There is a pagination tutorial on this site that is quite good.

 

http://forums.phpfreaks.com/page/tutorials/_/basic-pagination?pg=1

 

That should get you where you need to go, if you hit a roadblock, post back and we can help you resolve it.

There is a pagination tutorial on this site that is quite good.

 

http://forums.phpfreaks.com/page/tutorials/_/basic-pagination?pg=1

 

That should get you where you need to go, if you hit a roadblock, post back and we can help you resolve it.

 

Thanks for the link. I am basically following it, but I am stuck on one part.

 

You can see in my original code my SQL query takes the comments out of the DB and passes them to a function. Like so: 

$q = "SELECT * FROM threaded_comments WHERE parent_id = 0 ORDER BY id DESC";
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)):
	getComments($row);
endwhile;

In the pagination tutorial they do something like this: 

It looks like they put the data in an array and echo it from there. I really just want to pass it to my ajax function because I need the comments to update automatically. I got kinda stuck on how to do that. Any idea how to tweak this tutorial to make it work like my original query?

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo $list['id'] . " : " . $list['number'] . "<br />";
} // end while

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.