Simple SolarPHP Pagination Example

Being new to the SolarPHP scene I was trying to grasp some concepts around it, that for the lack of knowledge seem hard to do (and lack of any written articles found with google).

Pagination is implemented in SolarPHP by using a View Helper class called Pager.
At first I was perplexed about the fact that it took as parameters, among the usual number of items, number of pages etc., also the amount of pages, a minor detail that, usually, other pagination classes calculate automatically.

So I knew it should be a better option, other than passing the parameters by hand to the pager class and calculating the number of records by doing a count on the results returned by fetchAll.

The details are actually in the manual, but not so evident on a first pass (maybe for the lack of a concrete, idiot proof, example).

When creating the array for the fetchAll, as you can see here in the solarphp manual you have three nice options

  1. 'page'   => $page_number,
  2. 'paging' => $rows_per_page,
  3.    
  4. // do a followup query without limits to get a page count
  5. 'count_pages' => true,

I usually calculate my $page_number by using:

  1. $this->_request->get('page')?$this->_request->get('page'):1

(if someone has a better solution please let me know).
Update:
The built in get/post methods allow a second parameter that sets the default value, so the above code can be replaced by
$page = $this->_request->get('page', 1);
Thank Paul M. Jones for the heads up ;).

The $rows_per_page is pretty self explanatory, you have to say how many results you want to have listed on the page.

Now for the 'magic' part, the 'count_pages' option.
When set to true, it will automatically generate ALL the required options for the pager class, so that small 'problem' with amount of pages isn't a problem anymore.

Now, the thing is that by lack of attention I didn't read with enough attention this manual page so I had to go the hard way arround (like reading the files) to find the 'getPagerInfo' function that you can use on the
results generated by fetchAll();

That small, but important function allows you to do the following thing in your view file

  1. <?php echo $this->pager($this->list->getPagerInfo());?>

because it returns all the config options required by the pager

  1. array(8) {
  2.   ["count"] => int(10)
  3.   ["pages"] => int(3)
  4.   ["paging"] => int(4)
  5.   ["page"] => int(3)
  6.   ["begin"] => int(9)
  7.   ["end"] => int(18)
  8.   ["is_first"] => bool(false)
  9.   ["is_last"] => bool(false)
  10. }

Ta, da! Instant pagination that works as a charm.

For usage reference, here's my sample code:

  1. // public blog articles in descending order, all result pages
  2. $fetch = array(
  3.     'where' => array('blogs.status = ?' => 'public'),
  4.     'order' => 'blogs.created DESC',
  5.     'page'  => $this->page,
  6.     'paging' => 4,        
  7.     'count_pages' => true,
  8. );
  9.        
  10. // fetch all matching records
  11. $this->list = $this->_model->blogs->fetchAll($fetch);

with

  1. <?php echo $this->pager($this->list->getPagerInfo());?>

on the view page for generating the pagination.

Comments

Hi CoolGoose,

thank you for this helpful article! It's great to see how pagination works with Solar. I didn't thought it would be that simple :)

Thanks CoolGoose, hopefully this info will be included in the Manual in the future!

Pages

Add new comment