For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > July 2005 > Display search results on multiple pages









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Display search results on multiple pages
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.

CyberBlue

2005-07-25, 5:06 pm

"select * from tbl_name limit 10 offset 20;"

fetch 10 rows
start at the 20th record.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com