CakePHP - is there a way to use built-in pagination for an article's text?
I would like to use the built-in pagination in CakePHP (it's awesome!). But - is there a way to paginate a single field?
Example - if I have an article/story that's 10,000 characters - can I paginate that i开发者_如何学JAVAnto two 5,000 character pages without having to write my own pagination?
The default pagination works with SQL LIMIT
clauses, which works on the record level. You would have to at least override the Controller::paginate
method with your custom logic to do what you need it to do (just create a function paginate()
in your controller). You should be able to reuse the PaginatorHelper at least though, by setting all the right parameters that it'll pick up on. Try debug($this->params)
in the view or look at the default paginate
implementation to see what these variables are.
Break down:
- To paginate data normally, you do
$result = $this->paginate()
, which returns your paginated results. - Internally the
paginate()
function looks at the URL arguments (/page:42/sort:foobar
, etc.) to figure out which records it should return. paginate()
calculates which records to return with a bit of math based on the total number of records in the database, the records per page and the requested page number.- It also gives a bunch of information to the view about which records were returned, the current page we're on, how many pages there are etc.
- The PaginationHelper in the view reads this information to create the correct links for next/previous and the page numbers, each linking to a URL with
/page:xx/sort:yy
parameters. - Repeat the cycle.
What you're trying to do is hook into the process that the Controller::paginate()
function does. You need to implement your own version of it that doesn't return multiple results, but one "paginated" result. If the parameters it uses to do so and the parameters it sends back to the view are identical to the default implementation, the rest of the pagination logic should work exactly the same.
精彩评论