Skip to content
PHP Logo

How can the LIMIT parameter be used to paginate comments in a PHP forum?

To paginate comments in a PHP forum, you can use the LIMIT parameter in your SQL query to retrieve a specific number of comments per page. By adjusting the LIMIT parameter based on the current page number and the number of comments per page, you can display comments in a paginated manner.

php
// Assuming $page contains the current page number and $commentsPerPage contains the number of comments to display per page

$offset = ($page - 1) * $commentsPerPage;
$query = "SELECT * FROM comments ORDER BY comment_date DESC LIMIT $offset, $commentsPerPage";
// Execute the query and display the comments