PDA

View Full Version : displaying muliple result pages ?


davef101
02-10-2006, 09:16 PM
I'm trying to split a list of results into several result 'pages'.

i'm searching through a flat text file, picking out keywords.

but the problem i have, is i want to display only 25 results per page .. of a max of 100 results ..

i want the script to check the amount of results, then work out how many pages are needed, then automatically have links at the bottom of each page to the next list of results .. ie 1-25 .. 26-30 .. 31-45 etc ..

Any ideas ???

Thanks in advance.

script so far ..

#!/usr/bin/perl

$filename = "pricefiles/PriceFile.db";

local(@pairs,$pair,$val,$pos, $i);

if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $in, $ENV{'CONTENT_LENGTH'}+1);
}

$i = 0;
@pairs = split(/&/, $in);


foreach $pair (@pairs) {
# do the special character conversion
$pair =~ tr/+/ /;
$pair =~ s/%(..)/pack("c",hex($1))/ge;

$pos = index($pair,"=");
$name = substr($pair,0,$pos);
$val = substr($pair,$pos+1);

$i++;
$entry{$name} = $val;

}

############################################################
#



open(FILE,"$filename");
@lines = <FILE>;

foreach $line (@lines) {

chomp($line);

($P,$D,$PR,$MU) = split(/\|/,$line);

$LineCount++;

$PartNumber[$LineCount] = $P;

$PartDesc[$LineCount] = $D;

$PartPrice[$LineCount] = $PR;

$PartMarkup[$LineCount] = $MU;

}


close(FILE);

$entry{'selectManufacturer'} =~ tr/a-z/A-Z/;
$entry{'showModels'} =~ tr/a-z/A-Z/;

############################################################
#

print "Content-type: text/html\n\n";


open(FILE,"denso_sparkplugs/$entry{'selectManufacturer'}.txt");
@lines = <FILE>;

foreach $line (@lines) {

chomp($line);

($a,$b,$c,$d,$e,$f,$g,$h,$i) = split(/\|/,$line);

$lineNo++;

$model[$lineNo] = $a;
$cc[$lineNo] = $b;
$type[$lineNo] = $c;
$kw[$lineNo] = $d;
$engine[$lineNo] = $e;
$year[$lineNo] = $f;
$iridium[$lineNo] = $g;
$power[$lineNo] = $h;
$tough[$lineNo] = $i;

}


$lineNo = 0;

$found="";

foreach $line (@lines) {

# increment line counter
$lineNo++;

# does this line contain the term?
if ($line =~ /$entry{'showModels'}/) {



print <<End;

$entry{'selectManufacturer'} | $model[$lineNo] | $cc[$lineNo] | $type[$lineNo] | $kw[$lineNo] | $engine[$lineNo] | $year[$lineNo] | $iridium[$lineNo] | $power[$lineNo] | $tough[$lineNo] <BR><BR>

End


}

}

shyam
02-11-2006, 02:14 PM
one idea would be to not go throught the entire @lines again and figure out which ones to display and instead get only the matched items...something like this
foreach $line (@lines) {
chomp($line);
($a,$b,$c,$d,$e,$f,$g,$h,$i) = split(/\|/,$line);
$flag = undef;
foreach $element ($a,$b,$c,$d,$e,$f,$g,$h,$i) {
if ( $element =~ /$entry{'showmodels'}/ ) {
$flag = 1;
}
}
if ( $flag ) {
$lineNo++;
$model[$lineNo] = $a;
#...
$tough[$lineNo] = $i;
}
}
so $lineNo contains the number of matched items which will let you paginate...adding a hidden value for pageIndex will help in the navigation when the links below are clicked

$pageIndex = $entry{'pageIndex'};
$pageSize = 25;
if ( ($pageIndex * $pageSize) < $#lines && (($pageIndex + 1) * $pageSize) < $#lines ) {
for ( $i = $pageIndex * $pageSize; $i < ($pageIndex + 1) * $pageSize; $i++ ) {
print ''; # ....
} # endfor i
}

FishMonger
02-11-2006, 04:58 PM
Here are a numer of options:

Data::Page
http://search.cpan.org/~lbrocard/Data-Page-2.00/lib/Data/Page.pm

Data::Page::Navigation
http://search.cpan.org/~kazeburo/Data-Page-Navigation-0.02/lib/Data/Page/Navigation.pm

Data::Page::Viewport
http://search.cpan.org/~rsavage/Data-Page-Viewport-1.02/lib/Data/Page/Viewport.pm

Data::Page::Pageset
http://search.cpan.org/~chunzi/Data-Page-Pageset-1.02/lib/Data/Page/Pageset.pm

Data::Pageset
http://search.cpan.org/~llap/Data-Pageset-1.02/lib/Data/Pageset.pm

Also, the method that you're using to retieve/parse the form submission has been depreciated for 10 years. You reall should be using the CGI module:
http://search.cpan.org/~lds/CGI.pm-3.16/CGI.pm

By using the CGI module, your form parsing is reduced to this:

use CGI;

my $cgi = new CGI;
my %entry = $cgi->Vars;