| samseed 2005-07-24, 8:18 pm |
| Hi, here is the code I'm working with to query a database and display
the results on multiple pages. I am able to display the correct number
of page links and the total records, but I'm having trouble displaying
only 10 records per page.
use DBI;
use strict;
use warnings;
my $dbh = DBI->connect('dbi:ODBC:myTable');
....
#At this point, I have my query results
#in the following array, @results
my $result_count = 0;
while (my @results = $sth->fetchrow_array) {
...
++$result_count; #total number of records
}
#
# work out how many pages to show per page
#
my $pagesize = 10; #10 records displayed per page
my $pagecount = 0;
if ($result_count != 0) {
$pagecount = int($result_count / $pagesize);
if (($pagecount * $pagesize) != $result_count) {
$pagecount++;
}
}
#
# work out which is the first and last result to show
#
my $reqpage=1;
my $firstresult = (($reqpage - 1) * $pagesize) + 1;
my $lastresult = $firstresult + $pagesize - 1;
if ($lastresult > $result_count) {
$lastresult = $result_count;
}
At this point, I want to display my results (@results) 10 records per
page, but I'm stumped as how the looping logic should go. Any
suggestions would be greatly appreciated. Thanks.
|