Dear Monks, I'm currently working through the Perl Cookbook trying various recipes and am working on 14.6 - Viewing Data One Page at a Time. I have a database of around 25,000 lines and want to be able to limit the view so that a user can call a single word and view it from there whilst being limited to 50 results per page. The cgi script works but I fear I've gone horrifically wring on the fetching and presenting from the db part of the code
#!c:\perl\bin\perl.exe use strict; use warnings; use DBI; use CGI; my $q=new CGI; my %sm = $q->Vars(); my $word = $sm{concordance}; print $q->header(); print $q->start_html(-title=>'Dickens Concordance'); print qq[<form method="post"action="/cgi-bin/view.pl" name="concordanc +e">]; #code to create the scroll down box which selects from the db my $db = DBI->connect('dbi:mysql:lit:localhost', 'user', 'pw', {RaiseError => 1, AutoCommit => 1}); my $sth = $db->prepare("SELECT DISTINCT word FROM dickens"); $sth->execute; my @data; while (my @result = $sth->fetchrow_array) { push @data,$result[0]; } print $q->scrolling_list(-name=>"concordance", -value=>\@data, - +size=>30); $sth->finish; $db->disconnect(); print $q->submit(-value=>"Choose"); print qq[</form>]; #Count number of lines in db $db = DBI->connect('dbi:mysql:lit:localhost', 'user', 'pw'); my $row = $db->selectrow_arrayref('SELECT COUNT(*) FROM dickens'); my $count = $row->[0]; my $Page_Size = "50"; my $first = $q->param("start") || 1; my $last = $first + $Page_Size - 1; $last = $count if $last > $count; #Query the db my $results = $db->selectall_arrayref('SELECT word ,line FROM dickens +WHERE word="$concordance" LIMIT 50'); for (my $i=$first; $i<= $last; $i++) { my $user = $results->[$i-1]; printf("%d <br />\n", $i, $user); } #Create the next and previous pages my $prev_rec = $first - $Page_Size; $prev_rec = 1 if $prev_rec < 1; my $prev_link = sprintf('%s/%d', url(-full => 1), $prev_rec); my $next_rec = $last + 1; my $next_link = sprintf('%s/%d', url(-full => 1), $next_rec); if ($first == 1) { print "Previous"; } else { printf('<a href="%s">Previous</a>', $prev_link); } print "|"; if ($next_rec < $count) { printf('<a href="%s">Next</a>', $next_link); } else { print "Next"; }
I'd be grateful for some pointers as to where I've gone wrong so that I can get the results
word line number
word line number
presented on the screen. Many thanks in advance.

In reply to Viewing data one page at a time by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.