Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

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.


use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print "Content-type: text/html\n\n";

##### 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");
$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){
$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>]&nbsp;~;
print qq~[<a href="$script_url?$epp&page=$back">Previous $epp</a>]~;
if ($vres < $num && $vres != '') {print qq~&nbsp;[<a href="$script_url?$epp&page=$next">Next $epp</a>]~;}
print q~</small>~;

#### End html and other stuff
#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 and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho