i'm a beginner in cakePHP , and i wan't to create a custom pagination in cakePHP .
the function $paginator->numbers() ;
it displays the page numbers like this :
1 | 2 | 3 | 4 | ...
by looking in the options , there is some options to change the separator, to add a class of a style css ..Etc .
what i want is to have my pagination like this :
1-20 21-40 41-60 61-80 ... >>
some one has an idea about how to code it ?
EDIT :
i've created the custom paginator helper in : app/View/Helper/
,
and i've added my CustomPaginatorHelper
to $helpers
of my Controller
like this :
public $helpers = array('CustomPaginator', 'Html', 'Form', 'Js');
but i got this error :
Fatal error: Class 'PaginatorHelper' not found in /Applications/MAMP/htdocs/QRCode/app/View/Helper/CustomPaginatorHelper.php on line 2
it seems that he doesn't know the PaginatorHelper
!!!
Where i should add my custom Paginator
??
NB : your function numbers() will display just the format of the pagination : 1-20 21-40 ...etc , but without links to the pages i think :)
EDIT 2 :
i've added App::set('PaginatorHelper','/View/Helper/');
and i don't get this error anymore.
Now i try to call the numbers()
method of the custom paginator like this :
$this->CustomPaginator->numbers();
but i get this error :
Fatal error: Call to a member function numbers() on a non-object in /Applications/MAMP/htdocs/QRCode/app/View/Codes/index.ctp on line 71
what is the source of this error ? i've tried to add my customPaginatorHelper to the $helpers variable of my Controller but i still get the same error ; any ideas ?
thanks in advance
Answer for Custom Pagination in Cake PHP
A good example of when you would need this is if the underlying DB does not support the SQL LIMIT syntax. This is true of IBM's DB2. You can still use the CakePHP pagination by adding the custom query to the model.
Should you need to create custom queries to generate the data you want to paginate, you can override the paginate() and paginateCount() model methods used by the pagination controller logic. You also need to override the core paginateCount(), this method expects the same arguments as Model::find('count'). The example below uses some Postgres-specifc features, so please adjust accordingly depending on what database you are using.
Thanks see if this helps
I found even simpler solution, No Custom code needed and will be in a CakePHP's way of doing it, might not work in CakePHP 2.0 below versions cause I checked in 2.5...
For
Paginators
number()
method you can always override itsdefault
array()
which is like:now we can override these defaults anytime we want just use these attributes in your
options
array()
for example in your case:You can show answer here
Custom-Query-Pagination
You can also show another result here.
http://www.endyourif.com/custom-pagination-query-in-cakephp/
I found even an easier way of achieving the same thing. You will notice PaginationHelper extends AppHelper. So, if you copy any functions from the PaginatorHelper to the AppHelper calls and call it from the PaginationHelper, it will behave the same way and without any erros.
Usage
Code
The key thing to know here is that there is a paginator component (used in the controller) and a paginator helper (used in the view). What you're using is the PaginatorHelper class, which handles the rendering of the elements associated with pagination.
Unfortunately, there's no way of doing what you want to achieve with the PaginatorHelper. The best approach if you want to do this would be to extend the PaginatorHelper class and override the
numbers()
method to return what you want.I've taken a look at that particular method, and sadly it isn't nice - it's over 100 lines long! However, I've created a class that subclasses the PaginatorHelper and overrides the method. It's a lot of copy and paste as the original method is so long, and for that reason I've not put it directly in this answer.
You can view it here: https://gist.github.com/2037902
You also need to add
CustomPaginator
to the list of helpers in the controller.