In this tutorial, we will go through various techniques of using CakePHP Paginator helper. At the end of it, you will be able to use CakePHP pagination helper to accomplish your desired paginator (pagination links) easily.
Normally, when we use default options of Paginator, as covered in Cook Book, we do:
<!-- Shows the page numbers -->
<?php echo $paginator->numbers(); ?>
<!-- Shows the next and previous links -->
<?php echo $paginator->prev('« Previous', null, null,
array('class' => 'disabled')); ?>
<?php echo $paginator->next('Next »', null, null,
array('class' => 'disabled')); ?>
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $paginator->counter(); ?>
Let us look into the code line by line, so $paginator->numbers() will return page numbers similarly as below. As we can see, page numbers are wrapped inside tags. Current page's is added Css class "current". Clickable page number is wrapped inside a tag. And tags are separated by |.
<span class="current">1</span> | <span><a href="page-link">2</a></span>
$paginator->prev('« Previous', null, null,array('class' => 'disabled')) will return a previous navigation link similar as below. As we can see, when there is no previous page, the navigation text which is defined as the first paramter is wrapped inside a tag, and it is added the fourth parameter as the Css class("disable"). When there are previous pages, the navigation text is wrapped inside a tag with default Css class "prev", and it is again wrapped inside a tag.
if there is no previous page:
<span class="disabled">« Previous</span>
if there are previous pages:
<span><a href="page-link" class="prev">« Previous</a></span>
$paginator->next('Next »', null, null,array('class' => 'disabled')); return content almost as same as $paginator->prev('« Previous', null, null,array('class' => 'disabled')). It shows below:
if there is no next page:
<span class="disabled">Next »</span>
if there are next pages:
<span><a href="page-link/page:2" class="next">Next »</a></span>
At last, let us take a look at $paginator->counter(), this will basically return the page counter text as below. As we can see, it is default simple text of "current page of total page".
1 of 3
It is very common to have
<ul>
<li><a href="page-link/page:2" class="prev">« Previous</a></li>
<li><a href="page-link/page:1">1</a></li>
<li><a href="page-link/page:2">2</a></li>
<li class="current">3</li>
<li class="disabled">Next »</li>
</ul>
Firstly, let us create
<?php echo $paginator->prev('« Previous',array('tag'=>'li'),null,
array('class' => 'disabled','tag'=>'li'));
?>
<?php echo $paginator->next('Next »', array('tag'=>'li'), null,
array('class' => 'disabled','tag'=>'li'));
?>
We pass options to second paramter as options for active link, and fourth parameter as options for disabled link. We want to wrap both active and disabled link into
Next what we need to do is to generate based page numbers, it is very similar to what we have done above, and it is even simple, we do as below. As we can see, we passed in the same option 'tag'=>'li' into paramter, because paga numbers are not separated by active and disabled, one parameter does the trick. Additionaly, we have passed in 'separator'=> '', this option will overite the default | separator to be blank, because we don't want anything in between tags.
<?php echo $paginator->numbers(array('separator' => '','tag'=>'li')); ?>
In summary, what we have done to achieve our task is:
<ul>
<?php
echo $paginator->prev('« Previous', array('tag'=>'li'), null,
array('class' => 'disabled','tag'=>'li'));
echo $paginator->numbers(array('separator' => '','tag'=>'li'));
echo $paginator->next('Next »', array('tag'=>'li'), null,
array('class' => 'disabled','tag'=>'li'));
?>
</ul>
There are definitely much more customization we can, and we need to do when using Paginator helper, we will be adding more content here, and also please let me know what you want to know when using Paginator helper, we will be very happy to publish something useful for you. If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.