Custom Pagination in a PHP Cake

I am new to cakePHP and I am not creating custom pagination in cakePHP.

function $paginator->numbers() ; displays page numbers as follows:

 1 | 2 | 3 | 4 | ... 

Having looked at the parameters, there are some options for changing the separator to add the css..Etc style class.

I want my pagination to look like this:

 1-20 21-40 41-60 61-80 ... >> 

Does anyone have an idea on how to encode it?

EDIT:

I created a special paginator helper in: app/View/Helper/ ,

and I added my CustomPaginatorHelper to the $ helpers my Controller as follows:

 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 does not know PaginatorHelper !!! Where should I add my custom Paginator ?

NB: your function numbers () will only display the pagination format: 1-20 21-40 ... etc., but without links to the pages that I think :)

EDIT 2:

I added App::set('PaginatorHelper','/View/Helper/'); and I no longer get this error. Now I'm trying to call the numbers() method of a custom paginator as follows:

 $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 tried adding my customPaginatorHelper to the $ helpers variable of my controller, but I still get the same error; any ideas?

early

+4
source share
5 answers

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 use is the PaginatorHelper class, which handles the rendering of elements related to pagination.

Unfortunately, there is no way to do what you want to achieve with PaginatorHelper. The best approach if you want to do this is to extend the PaginatorHelper class and override the numbers() method to return what you want.

I took a look at this particular method, and unfortunately this is not nice - it's over 100 lines! However, I created a class that subclasses the PaginatorHelper and overrides the method. This is a lot of copies and pastes, since the original method is so long, and for this reason I did not specify 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.

+4
source

You can show the answer here.

Custom-query-pagination

Here you can also show a different result.

http://www.endyourif.com/custom-pagination-query-in-cakephp/

+1
source

Answer for custom pagination in a PHP volume

A good example of when you need this is that the underlying database does not support SQL LIMIT syntax. This is true for IBM DB2. You can still use CakePHP pagination by adding a custom query to the model.

If you need to create custom queries to generate the data that you want to paginate, you can override the paginate () and paginateCount () model methods used by the pagination controller logic. You also need to override core paginateCount (), this method expects the same arguments as Model :: find ('count'). The example below uses some Postgres-specific functions, so please configure them accordingly depending on the database you are using.

Thank you see if this helps.

+1
source

I found an even simpler way to achieve the same. You will notice that PaginationHelper extends AppHelper. So, if you copy any functions from the PaginatorHelper into the AppHelper calls and call it from the PaginationHelper, it will behave the same and without any errors.

Using

 $numbers_config = array( "before" => null, "after" => null, "separator" => "", "tag" => "li" ); echo $this->Paginator->customNumbers($numbers_config); 

the code

 // /app/View/AppHelper.php App::uses('Helper', 'View'); class AppHelper extends Helper { public function customNumbers($options = array()) { if ($options === true) { $options = array( 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last' ); } $defaults = array( 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null, 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...', 'currentClass' => 'current', 'currentTag' => null ); $options += $defaults; $params = (array)$this->params($options['model']) + array('page' => 1); unset($options['model']); if ($params['pageCount'] <= 1) { return false; } extract($options); unset($options['tag'], $options['before'], $options['after'], $options['model'], $options['modulus'], $options['separator'], $options['first'], $options['last'], $options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag'] ); $out = ''; if ($modulus && $params['pageCount'] > $modulus) { $half = intval($modulus / 2); $end = $params['page'] + $half; if ($end > $params['pageCount']) { $end = $params['pageCount']; } $start = $params['page'] - ($modulus - ($end - $params['page'])); if ($start <= 1) { $start = 1; $end = $params['page'] + ($modulus - $params['page']) + 1; } if ($first && $start > 1) { $offset = ($start <= (int)$first) ? $start - 1 : $first; if ($offset < $start - 1) { $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class')); } else { $out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator)); } } $out .= $before; for ($i = $start; $i < $params['page']; $i++) { $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator; } if ($class) { $currentClass .= ' ' . $class; } if ($currentTag) { $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass)); } else { $out .= $this->Html->tag($tag, "<a href='#'>".$params['page']."</a>", array('class' => $currentClass)); } if ($i != $params['pageCount']) { $out .= $separator; } $start = $params['page'] + 1; for ($i = $start; $i < $end; $i++) { $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator; } if ($end != $params['page']) { $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class')); } $out .= $after; if ($last && $end < $params['pageCount']) { $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last; if ($offset <= $last && $params['pageCount'] - $end > $offset) { $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class')); } else { $out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator)); } } } else { $out .= $before; for ($i = 1; $i <= $params['pageCount']; $i++) { if ($i == $params['page']) { if ($class) { $currentClass .= ' ' . $class; } if ($currentTag) { $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass)); } else { $out .= $this->Html->tag($tag, $i, array('class' => $currentClass)); } } else { $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')); } if ($i != $params['pageCount']) { $out .= $separator; } } $out .= $after; } return $out; } } 
+1
source

I found an even simpler solution, without the need for special code, and will be in CakePHP, may not work in CakePHP 2.0 below versions, because I checked in 2.5 ...

For the Paginators number() method, you can always override its default array() , which looks like:

 $defaults = array( 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null, 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...', 'currentClass' => 'current', 'currentTag' => null ); 

now we can override these default values ​​at any time when we just want to use these attributes in your options array() , for example, in your case:

  $this->Paginator->numbers( array( 'separator' => ' - ', 'after' => '', 'before' => '' )); 
+1
source

Source: https://habr.com/ru/post/1400591/


All Articles