Jump to content


Photo

How can I use pagination with comments?


  • Please log in to reply
5 replies to this topic

#1 Robert Zelník

Robert Zelník

    Full Member

  • Members
  • PipPipPip
  • 69 posts
  • 5

Posted 01 November 2011 - 02:07 AM

I would like to paginate comments created by Comments module.

I tried this:

http://processwire.c...rkup-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?

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3117

  • LocationAtlanta, GA

Posted 01 November 2011 - 09:53 AM

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.

#3 Robert Zelník

Robert Zelník

    Full Member

  • Members
  • PipPipPip
  • 69 posts
  • 5

Posted 02 November 2011 - 05:07 AM

It works well, Thanks Ryan.

#4 drilonb

drilonb

    Distinguished Member

  • Members
  • PipPipPip
  • 92 posts
  • 9

Posted 05 November 2011 - 09:52 AM

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> ";
	}





You can't just ask customers what they want and then try to give that to them. By the time you get it built, they'll want something new.

#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3117

  • LocationAtlanta, GA

Posted 07 November 2011 - 09:28 AM

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()) {


#6 drilonb

drilonb

    Distinguished Member

  • Members
  • PipPipPip
  • 92 posts
  • 9

Posted 07 November 2011 - 02:22 PM

Yes i am using now your correction and this is nice and work good , for this i love PW because always have too many choices to work with it,

Thanks Ryan,
You can't just ask customers what they want and then try to give that to them. By the time you get it built, they'll want something new.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users