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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
While(<database>) display results

 



najwilson
New User

Mar 19, 2003, 1:04 PM

Post #1 of 2 (357 views)
While(<database>) display results Can't Post

Does anyone have a clue how to solve this problem! I want to add code that will limit 10 or so, per page results to display, to tie in with the following Crazywhile(<database>)code without using a module..

open (DATABASE, "$datafile") || die "Can't Open $datafile";
while(<DATABASE>)
{


chop;
@results = grep(/$searchstr/i,$_);
if ($#results >= 0) {
foreach $i (@results) {
chomp ($i);

($sku, $category, $price, $short_description, $image,
$long_description, $shipping, $user1, $options) = split(/\|/, $_);


No help needed for this part - goes on to print result.......


davorg
Thaumaturge / Moderator

Mar 20, 2003, 1:10 AM

Post #2 of 2 (351 views)
Re: [najwilson] While(<database>) display results [In reply to] Can't Post

Something like this (untested)

Code
$per_page = 10; # records per page 
$page; # Page to be displayed
# (you'll need to pass this in somehow

open DATABASE, $datafile or die "Can't open $datafile: $!\n";

# get the records that match your search string
my @results = grep { /$searchstring/ } <DATABASE>;

# get the correct subset of the results
my @display = @results[($page-1)*$per_page .. ($page*$per_page)-1];

# Now just display the records in @display

Or you may be better off looking at the Data::Page and Data::PageSet modules from CPAN.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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