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

Newbie question!!
I have a script that accesses a pipe delimited flat file DB - It does a number of things to determine what should be output, and works fine. What I want to know is how do I print say, 5 of 10 records on one page and then provide a "next" link to the next 5?
Any help would be greatly appreciated
Thanks in advance.

Replies are listed 'Best First'.
Re: 5 of 10?
by Russ (Deacon) on May 13, 2000 at 00:37 UTC
    We've used a brute force method similar to this in a recent project:
    1. Pass (in a hidden field) the CurrentPage Number for each display
    2. Generate the entire list of "results"
    3. Take a slice from the list to just send the correct entries
    This is simple to code, and makes it easier to let the user jump straight to a particular page, and to choose how many results to see on each page (the bit of code below doesn't do either of these).
    my $CurPage = CGI::param('CurrentPage'); if (defined CGI::param('NextPage')){ $CurPage++; } elsif (defined CGI::param('PrevPage')){ $CurPage--; } print qq{ <input type="hidden" name="CurrentPage" value="$CurPage"> }; for my $Entry (@Entries[$CurPage * 5 .. $CurPage * 5 + 4]){ # Show each entry however you normally would } print '<a href="myscript.cgi?Prev=1">Previous</a>' if $CurPage; print '<a href="myscript.cgi?Next=1">Next</a>' unless $CurPage * 5 + 5 >= @Entries;
    Some notes:
    • The slice line looks a little goofy (adding 4 when we want 5 per page), I could write it as "+5 - 1" (and when we have a variable number of results, we do) but whatever...
    • This is pretty wasteful (we do the entire database lookup for each page, then throw away what we don't want)...
    Hope this helps a little...

    Russ

Re: 5 of 10?
by btrott (Parson) on May 13, 2000 at 00:01 UTC
    How are you storing the data in your script? I assume you have some sort of data structure? If you're storing the data in an array, just print out a next link with a parameter like "start=5"; then in your script, just print out the array elements from the start value (5, in this case) to the limit of records per page (the limit, in your case, would seem to be 5).

    If you're not storing the data in a data structure, but are instead printing out each line in a while loop as you read from the file, you can check $. for the current line number, and only print the line if the line number is between 5 and 10. Or something such. You could last out of the loop after you hit the limit.

    Does this make sense? There's no magic involved, really--you just have to code it up yourself.

    Also, check out Randal's WebTechniques column on this: Giving 'more'.

Re: 5 of 10?
by httptech (Chaplain) on May 13, 2000 at 05:42 UTC
    Look on CPAN for HTML::Pager - It was designed to do just this.
RE: 5 of 10?
by merlyn (Sage) on May 13, 2000 at 04:08 UTC
RE: 5 of 10?
by Maqs (Deacon) on May 13, 2000 at 12:22 UTC
    The principal solution is to export the number of the last entry through post, i.e.: a href="myscript.cgi?entry=5"
    Then you can use the following routine in your script:
    if ($entry >=5) {my $backnum= $entry-5} else {my $backnum=0}; my $nextnum=$entry+5; while ($entry % 5) { #do your output stuff ; $entry++; if ($backnum) { print "<a href=\"myscript.cgi?$backnum\">Previous</ +a>"}; }; print "<a href=\"myscript.cgi?$nextnum\">Next</a>"}; #other stuff...
    However, if you would not use CGI module at all, you may parse parameters as $ARGV[0]
    You might make a check for the nnumber of entries not to make your Next link to the enmpy page.
    --
    With best regards
    Maqs.
      Not to be a noodge, but isn't that get that pases data through the URL, not post?

      Either way, I'm all about using argv[0] for this sort of thing-- it works great for situations where you need to pass data but don't really need the functionality of a form, and I tend to botch it much less frequently than I botch get and post.

      -T.McWee

      The Autonomic Pilot

Re: 5 of 10?
by Anonymous Monk on May 14, 2000 at 21:18 UTC
    fffg