Posted by TedH (TedH), 23 November 2007
For quite a while I have been trying to get a decent pagination for text files. I probably should have posted here earlier, but felt I might be overusing the forum.
I have a next-back type solution (see below) that I've used in various forms for a number of years. However, this can be a bit of a pain if you've got scores of records. FFDB's are used (though it works on text lines as well) as nearly everything I do is not really big enough to warrant SQL or MYSQL.
Code:#!/usr/bin/perl
use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print "Content-type: text/html\n\n"; &parse_form;
##### Variables $database="file.txt"; # name of file $epp = "4"; # entries per page $ppts = "5"; # max number of page links to show each time $script_url = $ENV{SCRIPT_NAME}; # script location for page navigation $page = $input{'page'}; # variable for page navigation;
&header; # header must go here
### Open database and read in records open (ORGDB,"$database"); @ODB=<ORGDB>; $count++ while <@ODB>; $numrecs = "$count"; close (ORGDB);
print qq ~<b>There are $numrecs records.</b><hr>~;
## do something with database # Print records foreach $rec (@ODB){ $num++; $vres = ($page + 1) * $epp; # view results if ($num > ($vres - $epp) && $num <= $vres) {print "$rec<br>\n";} } ## end foreach
## do print out print qq~<small>~; $first = $page[-1]; $back = $page - 1; $next = $page + 1;
print qq~<hr>~; if ($page != 0) { print qq~[<a href="$script_url?$epp&page=$first">Beginning</a>] ~; print qq~[<a href="$script_url?$epp&page=$back">Previous $epp</a>]~; } if ($vres < $num && $vres != '') {print qq~ [<a href="$script_url?$epp&page=$next">Next $epp</a>]~;} print q~</small>~;
#### End html and other stuff &footer; #Parse form sub parse_form { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); if (length($buffer) < 5) {$buffer = $ENV{QUERY_STRING};} @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $input{$name} = $value; } }
sub header {print qq ~<html><head><title></title><style>body {font-family:verdana;}</style></head><body>~;} sub footer {print qq ~</body></html>~;} # end |
|
The main thing I need to know how to do, is make this so (instead of the next back) I have something like:
1,2,3,4,5 ....
Where the link numbers change depending on which result you're viewing. The variable "$epp" controls the number of records per view. $ppts defines how many links would show at a time.
This page is a thread posted to the opentalk forum
at
www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow
this link.