Skip to content

Instantly share code, notes, and snippets.

@kongondo
Forked from somatonic/paginator.php
Last active December 18, 2015 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kongondo/5728078 to your computer and use it in GitHub Desktop.
Save kongondo/5728078 to your computer and use it in GitHub Desktop.
manual pagination example for in memory page arrays
<?php
/**
* include paginator class from ProcessWire core, $config->paths->Modulename can
* be used to get the path of any module in PW.
*/
require_once($config->paths->MarkupPagerNav . "PagerNav.php");
/**
* create a new PageArray object to fill in results
*/
$pa = new PageArray();
$res1 = $pages->find("template=xyz, title%=space");
$res2 = $pages->find("template=abc, title%=hal");
/**
* add the results (PageArray) to the container (PageArray), doubles will get removed
*/
$pa->import($res1);
$pa->import($res2);
/**
* some variables for paginator
*/
$baseUrl = $page->url;
$limit = 4;
/**
* $input->pageNum returns the /page[n] from the url
* this requires page numbers enabled on the template for the page
*/
$start = ($input->pageNum - 1) * $limit;
$total = $pa->getTotal();
/**
* create a new PageNav object with the required inputs to
* generate a Pager object to create the markup
*/
$pagerNav = new PagerNav($total, $limit, $input->pageNum);
/**
* now we just get the Pager Array by calling getPager()
*/
$pager = $pagerNav->getPager();
/**
* construct paginator markup by looping the Pager Array
*/
$pagerMarkup = '';
foreach($pager as $link) {
$class = $link->pageNum == $input->pageNum ? 'on' : ''; // is it the current page?
if($link->type == 'separator') $item = '…'; // is it a separator
else $item = "<a class='$class' href='{$baseUrl}page{$link->pageNum}/'>$link->label</a>"; // or a normal link
$pagerMarkup .= "<li>$item</li>";
}
/**
* output paginator markup
*/
echo "<ul class='pager'>" . $pagerMarkup . "</ul>";
echo "total: $total<br/>";
echo "start: $start<br/>";
echo "limit: $limit<br/>";
/**
* output the merged page array result with using the start and limit evaluated above
*/
echo '<ul>';
foreach($pa->find("start=$start, limit=$limit") as $p){
echo "<li>$p->title</li>";
}
echo '</ul>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment