Jump to content

How can I use pagination with comments?


Robert Zelník
 Share

Recommended Posts

I would like to paginate comments created by Comments module.

I tried this:

http://processwire.com/api/modules/markup-pager-nav/

Determine what template(s) you want to use pagination with. Go to Admin > Setup > Templates > [Your Template] > Advanced, and check the box for: Allow Page Numbers. Save.

I didn't find the checkbox. I assume it's because the comments are not pages. Am I right? Is there any other way to paginate the comments?

Link to comment
Share on other sites

There is no built-in support for pagination of comments at present. Though it's certainly possible to paginate them, but not without doing the pagination yourself. The $page->comments value is a CommentsArray, which is a WireArray. You can see all the WireArray methods here:

http://processwire.com/api/arrays/

So your comments pagination would probably be something like this:

<?php

$limit = 10; 
$start = ($input->pageNum - 1) * $limit; 

$comments = $page->comments->slice($start, $limit); 
echo $comments->render(); 

if($input->pageNum > 1) {
    echo "<a href='./page" . ($input->pageNum - 1) . "'>Previous Page</a> ";
}

if($start + $limit < count($page->comments)) {
    echo "<a href='./page" . ($input->pageNum + 1) . "'>Next Page</a> ";
}

The above is an approximation written in the browser, so may not be a working example, but should be close.

  • Like 1
Link to comment
Share on other sites

From this form i am using it for other reason and its working perfect thanks Ryan making pagination for any of pages with children,


<?php 
$limit = 3; 
$start = ($input->pageNum - 1) * $limit; 

$children = $pages->get("/events/")->children->slice($start, $limit);
        foreach($children as $events) {  
        echo" <div class='event'><p><a href='$events->url'>{$events->title}</p> </a> <h3>{$events->datapickup}</h3> </div>";}			

if($input->pageNum > 1) {
	echo "<a href='./page" . ($input->pageNum - 1) . "'>Previous Page</a> ";
}

if($start + $limit < count($pages->get("/events/")->children)) {
	echo "<a href='./page" . ($input->pageNum + 1) . "'>Next Page</a> ";
}

Link to comment
Share on other sites

drilonb, in your case it would be more efficient to use PW's built in pagination. The only reason Robert Zelnick had to use the method he did is because he wasn't working with pages. Pages are built for pagination, so they have things to make it more efficient and easier. When you call 'children' without any selector, it has to load all the children. If there are hundreds or thousands, that could be lot of unnecessary overhead.

This is a preferable way to do it with pages:

// only has to load $limit children
$pages->get('/events/')->children("start=$start, limit=$limit");

Whereas this is not a good way to do it with pages:

// has to load ALL children
$pages->get('/events/')->children->slice($start, $limit); 

Also, using the "start=" isn't necessary when using built-in pagination because PW does that automatically behind-the-scenes from the page number. Though you can certainly use it, but it's just not technically necessary.

Lastly, when using built-in pagination for your 'next page' link, you want to use $children->getTotal() rather than count($children). The reason is that count($children) would only have $limit items, whereas $children->getTotal() would have the total number of items if it had loaded them all. So you'd want to replace this:

if($start + $limit < count($pages->get("/events/")->children)) {

…with this:

if($start + $limit < $children->getTotal()) {

And there's no need to keep your own $start or $limit variables since PW is already keeping them for you. So you could just do this instead to determine whether to show a 'next page' link:

if($children->getStart() + $children->getLimit() < $children->getTotal()) {
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...