Let's say that you want to retrieve the records from a table, via query like:
SELECT * FROM customer $LIMIT_CLAUSE
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:
  1. the number of pages in the resultset
  2. the number of records to skip for the current SELECT statement
  3. the number of the first and last record in the set

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:

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
and I called the Model from a CGI::Application controller like so:
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); }
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?

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.


In reply to kickstarting a paged result set by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.