This should get you going... I have shown you how to do page ranges just for the hell of it.
# 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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.