# http://somesite.com/cgi-bin/display.pl?current_page=12
# display.pl
#!/usr/bin/perl -w
use strict;
use CGI;
#use DBI;
my $SCRIPT = 'http://somesite.com/cgi-bin/display.pl';
my $RECS_PER_PAGE = 10;
my $q = new CGI;
print $q->header;
#my $dbh = DBI->connect( etc.... );
if ( $q->param('start')) {
display_this_db_section( $q->param('start'), $q->param('end'), $q-
+>param('current_page') );
}
else {
my $count = get_record_count();
my $page = $q->param('current_page') || 1;
my $html = get_current_page($page);
$html .= generate_links($count, $page );
print $html;
}
exit 0;
#### SUBS ####
sub display_this_db_section {
my ($start, $end, $current_page ) = @_;
$current_page ||= $start;
my $count = get_record_count;
die_nice ( 'Invalid' ) if $start > $count
or $start < 1
or $end > $count
or $end < 1
or $start > $end
or $start > $current_page
or $end < $current_page;
print "<h3>Here is $current_page of $start-$end<h3>\n";
print "<p>", generate_links( $count, $current_page )
# blah
}
sub generate_links {
my ($count, $current_page ) = @_;
my $html = '';
for ( my $start = 1; $start <= $count; $start += $RECS_PER_PAGE )
+{
my $end = $start + $RECS_PER_PAGE -1;
$end = $count if $end > $count;
$html .= ( $start <= $current_page and $current_page <= $end )
+ ?
"[$start-$end] " :
qq!<a href="$SCRIPT?start=$start&end=$end">[$start-$e
+nd]</a> \n!;
}
return $html;
}
sub get_current_page {
my $page = shift;
# validate page, get data, format, return html
return "<p>This is the content for page $page<p>\n";
}
sub get_record_count {
# get count of records, simulate here
return 42;
}
sub die_nice { print shift and exit }
Prints
This is the content for page 1
[1-10] [11-20]
[21-30]
[31-40]
[41-42]
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|