Now, assuming this returns more records than you would want to display on a single page, then a module like Data::Page or Data::Pageset is useful, because it calculates all the things you need:SELECT * FROM customer $LIMIT_CLAUSE
Now, initializing a Data::Page object requires telling it the total number of entries. Now once you have your paged data, you can setup the links to each page so that Data::Page can calculate the proper parameters for your LIMIT clause.
BUT! On the first request for the result set, you do not know the total number of entries. So, you have to get them in addition to the first page of data.
The way that I did this was setup my model class so that it conditionally defined $LIMIT_CLAUSE. If it received a $pager object, then it performed a limited select. Otherwise, it did a full select:
and I called the Model from a CGI::Application controller like so:sub all { my ($app, $pager) = @_; my $LIMIT_CLAUSE; if ($pager) { $LIMIT_CLAUSE = sprintf "LIMIT %d, %d", $pager->skipped, $pager->entries_per_p +age; } my $query =<<"EOSQL"; SELECT * FROM table_name $LIMIT_CLAUSE EOSQL
It seems wasteful to do 2 queries in a row that way, but hopefully database caching will reduce that penalty to just the first time it occurs?sub affil { my $app = shift; my $aff_model; my $pager = Data::Page->new; my $entries_per_page = 2; $pager->entries_per_page($entries_per_page); # either call database to get all rows or use CGI query parm my $total_entries = $app->query->param('total_entries') || Model::Aff::all($app)->rows ; $pager->total_entries($total_entries); my $current_page = $app->query->param('current_page') || 1 ; $pager->current_page($current_page); $aff_model = Model::Aff::all($app, $pager); View::Aff::Main::render($app, $aff_model, $pager); }
The other option was to use the non-paged resultset (the first time the page was hit without knowledge of total rows in dataset) and modify the View code such that it rendered differently based on whether or not the results were paged.
But that would have been more tedious.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: kickstarting a paged result set
by mr_mischief (Monsignor) on Feb 03, 2009 at 22:56 UTC | |
|
Re: kickstarting a paged result set
by CountZero (Bishop) on Feb 04, 2009 at 06:42 UTC | |
|
Re: kickstarting a paged result set
by Anonymous Monk on Feb 07, 2009 at 02:28 UTC |