Simple example of how to use the pagination api?

Permalink 1 user found helpful
Does anyone have a simple example of how to use the pagination api?
I'm querying a db table and would like to spread the result over multiple pages.

Thanks!

 
beebs93 replied on at Permalink Reply
beebs93
A basic example:

// Retrieve a list of immediate child pages
$pl = new PageList();
$pl->filterByParentID($c->cID);
$pl->sortByDisplayOrder();
$pl->setItemsPerPage(25);
// Set paginator
if($pl->getSummary()->pages > 1){
   $paginator = $pl->getPagination();
   $strPages = $paginator->getPages();
}
// Get paginated pages loop through each company listing
$pages = $pl->getPage();
// Loop through the $pages array here
// Display the pagination
echo !empty($strPages) ? $strPages : '';
lemonjules replied on at Permalink Reply
Hi beebs93,
Thanks for your example but I'm still very confused. Is the pagination helper built to work with pages only?
what I need is as follows:
I have a table with a 30 items, I would like to query the database and return 10 items per page with "next" and "previous" links.
In Drupal I was able to do that with the pagination class.

Thanks!
beebs93 replied on at Permalink Reply
beebs93
I've only ever used the pagination class for pages. What kind of items are you retrieving from the C5 database?
lemonjules replied on at Permalink Reply
I have create a table of paintings, I use a web service (SOAP) to update the table.
fields: (id)(title)(artists)(date_added)
beebs93 replied on at Permalink Reply
beebs93
I'll start poking around the related libraries, but I think one of the more experienced C5 developers would have a better understanding.

I'll post anything useful I come across :)
jordanlev replied on at Permalink Best Answer Reply
jordanlev
No, you can also use that for things besides pages (pages is just the most common use case in the c5 world). The PageList class builds off of a lower-level class called "DatabaseItemList", which you can also use for your own database table to get lots of fun features such as pagination. There's some very terse documentation here:
http://www.concrete5.org/documentation/developers/system/searching-...

I think basically what you do is load up this object, pass your SQL query to it, then it magically handles pagination for you. For example:
Loader::library('item_list');
$paintings = new DatabaseItemList();
$sql = "SELECT * FROM paintings";
$paintings->setQuery($sql);
if ($paintings->getSummary()->pages > 1) {
  $paginator = $paintings->getPagination();
  $pageListing = $paginator->getPages();
}
$onePageOfPaintings = $paintings->getPage();
//etc...


I haven't tested this out so I'm not 100% sure it works perfectly, but should get you most of the way there hopefully.

-Jordan
lemonjules replied on at Permalink Reply
Right on the money. Exactly what I was looking for.
Thanks to all of you.
tinywings replied on at Permalink Reply
I keep getting this error:
Fatal error: Call to protected method DatabaseItemList::setQuery() from context 'SinglePageController' for my single pages controller- what function is this supposed to be in?
jordanlev replied on at Permalink Reply
jordanlev
Oops, sorry about that. Looks like you need to create a new class first -- same basic idea, just the code is a bit more verbose:
Loader::library('item_list');
class PaintingList extends DatabaseItemList {
   public function __construct($query) {
      $this->setQuery($query);
   }
}
$sql = "SELECT * FROM paintings"
$paintings = new PaintingList($sql);
if ($paintings->getSummary()->pages > 1) {
  $paginator = $paintings->getPagination();
  $pageListing = $paginator->getPages();
}
$onePageOfPaintings = $paintings->getPage();
//etc...