Click to See Complete Forum and Search --> : Displaying search results by page


squalle
10-20-2007, 03:09 AM
Ok, I've searched and searched. I've tried modifying other scripts, etc. Can't seem to get this to work. :(

What I'd like to know is how to display search results from a ff database by page, so if there are 22 results, it will display 5 at a time. The problem I run into while looking for tutorials, etc, is that most people tend to overdo it. I would like some insight on how to do this very simply.

The testing database I have is small, 22 items, 2 fields per line. Like this:

1|Item 1
2|Item 2
...
21|Item 21
22|Item 22


And the HTML on the page would be just a simple table row for each line of the database, like:


<table>
<tr>
<td>Item 1</td>
</tr>
...
<tr>
<td>Item 5</td>
</tr>
<tr>
<td><a href="...">NEXT</a></td>
</tr>
</table>


I would love it if someone could simplify the code snippet to display 5 items at a time with Next and Back links to the next page.

Thanks,
Squalle

CyCo
10-20-2007, 07:18 PM
Here's a little example that you can play with. Mildly tested.#!/usr/bin/perl

use strict;
use Tie::File;
use Fcntl ':flock';
use warnings FATAL => 'all';
use CGI::Carp 'fatalsToBrowser';
use CGI::Pretty qw/:standard :no_xhtml/;

$CGI::Pretty::INDENT = ' ';

my $count = 1;
my $items_per_page = 5;
my $page = param('page') || 1;
my $index = $page * $items_per_page + 1;
my $ref = tie my @dat, 'Tie::File', 'your_flatfile.txt' or die $!;

$ref->flock(LOCK_SH) or die $!;

for (@dat) {
++$count;
my ($first, $second) = split '\|';
push @_, Tr(td($second)) if $count > $index - $items_per_page && $count <= $index;
}

print header, start_html('Simple Back/Next'), table(@_), display_linx(), end_html;

sub display_linx {
use constant S => script_name;

my $linx = '';
my ($next, $back) = ($page + 1, $page - 1);

$linx .= a({-href => S . "?page=$back"}, 'back') unless $page == 1;
$linx .= a({-href => S . "?page=$next"}, 'next') if $index < $count;

return p($linx) if $linx;
}

squalle
10-20-2007, 10:16 PM
CyCo,

That works! Thanks a bunch. Finally, some code that works great without going over the top.

The code is still a little over my head, as I'm not up on all the new ways of doing things, but it's working, and that's what matters right now.

Thanks again,

Squalle:D

bluestartech
10-22-2007, 07:03 AM
you could also use HTML::Pager, which handles splitting pages of search results, it requires the HTML::Template module, the included docs are quite thorough...it is a more elegant solution in the long run as you can seperate design/layout from application function.

Diarmuid Ryan
Blue Star Web Design, Tipperary, Ireland (http://www.bluestar.ie)