Here's an explanation of how I did custom pagination.
In app/config/bootstrap.php I include a global functions file which is
located in app/webroot/includes/
if (file_exists("includes/global_functions.php"))
require_once("includes/global_functions.php");
In this file are my pagination functions:
http://bin.cakephp.org/view/522546645
I use these functions for all my pagination needs, e.g. paginating a
table of texts in my texts/index actions.
In texts_controller I have a var to determine the number of records
per page:
var $perpage = 50;
and a function to determine the number of texts:
function nrecords() {
$res = $this->Text->query("select count(*) as n from texts");
return $res[0][0]['n'];
}
The index action looks like this. Only four lines of code are needed
for the pagination, and they result in a LIMIT clause ($limit) which I
attach to my sql query. Also important is that the action receives the
requested page number $page. As you can see in the beginning of the
code I'm sorting queries on the result of mysql functions, which is
why I needed custom queries and pagination to begin with.
function index($page = 1, $sort = 'title', $dir = '') {
// determine sort order
$sortdir = ($dir == 'down') ? "desc" : "asc";
if ($sort == 'year') {
$order = "order by ifnull(texts.year,albums.year) {$sortdir},
concat(artists.sort, artists.name) {$sortdir}, texts.title
{$sortdir}";
} elseif ($sort == 'artist') {
$order = "order by concat(artists.sort, artists.name)
{$sortdir}, texts.title {$sortdir}";
} else {
$sort = 'title'; // default
$order = "order by texts.title {$sortdir}, concat(artists.sort,
artists.name) {$sortdir}";
}
// paginate
$nrecords = $this->nrecords(); // get total number of records
$perpage = $this->perpage; // get number of records per page
$npages = pag_npages($nrecords, $perpage); // determine
number of pages
$limit = pag_sqllimit($page, $perpage, $npages); // create the
"LIMIT" clause for SQL
// query
$this->set('texts', $this->Text->query("
select texts.id, texts.title, texts.slug, texts.audio,
texts.video,
length(texts.text_transl) as transl,
length(texts.text_phonetic) as phonetic,
artists.id, artists.name, artists.sort, artists.name__AW,
artists.slug,
ifnull(texts.year,albums.year) as year
from texts
left join albums on texts.album_id = albums.id
left join artists on texts.artist_id = artists.id
{$order}
{$limit}
"));
// pass on the sort and pagination info to the view
$this->set('page', $page);
$this->set('npages', $npages);
$this->set('nrecords', $nrecords);
$this->set('sort', $sort);
$this->set('dir', $dir);
// layout
$this->set('no_right_col', true);
}
Now in my view I add pagination links with:
$pag_numbers = pag_numbers($page, $npages, "/tekste/", ($dir) ? "/
{$sort}/{$dir}" : "/{$sort}", 3, true);
this returns a full row of page browsing links, e.g. "Previous | 1 |
2 | 3 | 4 | 5 | Next". The source of the pag_numbers() function
explains the parameters. I could also use pag_prev() and pag_next() to
just have Previous and Next links, but the final true parameter of
pag_numbers means the Previous and Next links get added around the
page numbers.
That's it. You can see this particular paginated table in action at
http://www.antwerps.be/teksten .
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---