CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
Search Results 10 by 10



Dec 20, 2000, 7:54 PM

Post #1 of 3 (49527 views)
Search Results 10 by 10 Can't Post

 This is to help someone who has been having trouble understanding how to split your search results into pages, 10 by 10. So if your searching a database of page titles, and in the database is only the title|description|url|, using the pipe as a delimiter, post and explain your code to split your search results into pages of 10 results per page. Make sure you don't have a next button when your on the last page too.

(Moderators participate!!)



Dec 21, 2000, 8:17 AM

Post #2 of 3 (49518 views)
Re: Search Results 10 by 10 [In reply to] Can't Post

Assuming your database only gets information appended to it (not changed in the middle or beginning), here is a simple solution for flat file databases:

use CGI; 
use strict;

my $q = CGI->new;
my $DB = "/path/to/file.db";
my $pattern = $q->param('pat');
my ($pos, @matches, $last_loc, $more);

open FILE, $DB or die "can't read $DB: $!";

# jump ahead in file to continue searching
seek FILE, $pos, 0 if $pos = $q->param('pos');

while (<FILE>) {
chomp(my @fields = split /\|/);
# assume a match if the description matches
if ($fields[1] =~ /$pattern/o) {
push(@matches, \@fields);
$last_loc = tell(FILE) - length; # get position
last if @matches == 11; # yes, 11
close FILE;

# figure out if there's more
$more = @matches == 11 and pop @matches;

more_button($last_loc,$pattern) if $more;

sub display { ... }

sub more_button {
my ($pos,$pat) = @_;
print qq{See <a href="prog.cgi?pos=$pos&pat=$pat">more</a>.\n};

Most of the code is self-explanatory. The nice trick, though, is using a file offset to jump ahead in the database file -- we change the place where we start looking.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Dec 21, 2000, 1:24 PM

Post #3 of 3 (49516 views)
Re: Search Results 10 by 10 [In reply to] Can't Post

 Yes I aggree, it is. It also proves to be faster than using line counts to position your self. Well Done Japhy. I tested this on my pc (win 98 w/ Active perl 5.6 PWS)

I did make a few changes, one small one to be presice, when you go to the next results, the number of bytes is 1 too many and gulps up the first letter when only printing the titles and shifting to the next results. So I just substracted one byte from $last_loc and it was fine. It's very fast too, but when you go to add how many search results it found and what it's displaying, (Found 34 Results: Showing 20-30 kinda thing), it may slow down, and that's when using a line count makes things easier. Then you don't have to worry about going back and trying to find line numbers. You Could find what line numbers your displaying with the code, but not the total found, sorry if I'm wrong Japhy.

I attached what I tested.



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives