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>'; |
<?php | |
/** | |
* here is a example with using the built in pager | |
* we can use $a->setTotal($n) to configure page array to work with pager | |
*/ | |
// create new page array for storing pages | |
$pa = new PageArray(); | |
$res1 = $pages->find("template=mytemplate"); | |
$res2 = $pages->find("template=house"); | |
// import the found page | |
$pa->import($res1); | |
$pa->import($res2); | |
// configuration for pagination needed | |
$limit = 10; | |
$total = $pa->count(); | |
$start = ($input->pageNum-1)*$limit; | |
// output limited list we filter from the complete page array | |
foreach($pa->filter("sort=-created, limit=$limit, start=$start") as $p){ | |
echo "<p>$p->title</p>"; | |
} | |
// page array let's you set the parameters for pagination manually | |
$pa->setLimit($limit); | |
$pa->setTotal($total); | |
$pa->setStart($start); | |
// now the renderPager() on the page array works as usual | |
echo $pa->renderPager(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment