http://www.perlmonks.org?node_id=273084

artist has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks
Here is my code-cut.
my $result = Database->get_results($query); Display->results($result);
and package Display uses HTML::Template to display the $result. It works fine currently. It displays 5000+ records.

Q: How do I use HTML::Pager or another module such that I can display data page by page, may be 100 at a time.

Thank you,
artist

Replies are listed 'Best First'.
Re: Database Data Page
by cleverett (Friar) on Jul 11, 2003 at 00:14 UTC
    There's major issues with HTML::Pager I see up front: it wants the whole data set up front, and it uses CGI.pm, not quite the thing when you have HTML::Template, IMO.

    Since retrieving the entire data set every time through is grossly inefficient, you likely will end up doing one or both of two things: retrieving just the rows you need or caching the results someplace.

    Many dialects of SQL will let you specify the rows you need. For instance Mysql has a LIMIT clause:

    SELECT * FROM bannerlog.log LIMIT 100, 100;

    which says return only the rows starting at the 101st row and going 100 rows from there.

    If we were saving state using HTML form variables and using CGI::Request to fetch form parameters (using mod_perl, I'd use Apache::Request) our code would then end up something like:

    use CGI::Request; my $req = new CGI::Request; my $rows_per_page = 100; my ($first_row, $command) = map { $req->param($_) } qw/first_row command/; if ($command eq 'previous_page') { $first_row -= $rows_per_page; $first_row = 0 if $first_row < 0; } elsif ($command eq 'next_page') { $first_row += $rows_per_page; } my $result = Database->get_results($query, $first_row); Display->results($result, $first_row);

    good luck!

Re: Database Data Page
by little (Curate) on Jul 11, 2003 at 17:30 UTC

    what about using LIMIT as a native SQL language construct?
    SELECT * FROM tableName LIMIT 0, 10
    or in another way:
    SELECT $whatever and $whomever FROM $sometable LIMIT $minDisplaySetOff, $maxDisplayRowsPerPage
    actually it should read $maxDisplayRowsPerResultSet

    Have a nice day
    All decision is left to your taste