I am once again in need of Perl Monk wisdom. I am having a hard time wrapping my brain around database paging. I have found a few examples on the internet but they don't quite work for me.

With every example I have tried, the problem is always the same. I can output the database results and the correct number of page links depending on the results of the page. The problem is when I click on a page link, it goes to the next page but with no results and the page links disappear. Here is my code:

if ($ENV{REQUEST_METHOD} eq "GET") { &header(); &display(); } else { &header(); &display(); } ######################### ### Print Header #### ######################### sub header { $cgi_object=new CGI; print $cgi_object->header('text/html'); } ######################### ### Display Page ### ######################### sub display { print qq~ <!DOCTYPE html> <html> <head> <title>Search</title> </head> <body> <form name="login" method="POST" action="/cgi-bin/search.cgi" /> <input type='submit' name='submit' id='submit' /> <h2>How Many Results Would You Like To See Per Page?</h2> <select name="per_page" id="per_page"> <option value="25">25</option> <option value="50">50</option> <option value="75">75</option> <option vaule="100">100</option> </select> <br /><br />~; my $query = new CGI; #$pagesize=$query->param('per_page'); my $reqpage = $query->param('reqpage') || '1'; #Get total amount of rows from db $dbh=DBI->connect($connectionInfo,$user,$passwd); my $num_rows= $dbh->selectrow_array('select count(name) from video + order by name'); my $num_results = $query->param('per_page'); # calculate the number of pages to show my $pagecount = int($num_rows / $num_results); if (($pagecount * $num_results) != $num_rows) { $pagecount++; } # calculate which results to show in the page my $firstresult = (($reqpage - 1) * $num_results) + 1; my $lastresult = $firstresult + $num_results - 1; if ($lastresult > $num_rows) { $lastresult = $num_rows; } # sql limit starts at 0 my $start_point = $firstresult - 1; my $sth = $dbh->prepare("Select name,path from video order by name + LIMIT $start_point,$num_results"); $sth->execute(); while (@row = $sth->fetchrow_array()) { my $name = $row[0]; my $path = $row[1]; $path =~ s/'/%27/g; $dsp = substr $path, 27, 255; print "<a href='/media/$dsp'>$name</a> <br />"; $count=$count+1; } $sth->finish(); # page links my ($prev_link, $next_link, $pagelinks, $pageno, $thislink, $pages +ize); my $prev_page = $reqpage - 1; my $next_page = $reqpage + 1; if ($reqpage == 1) { $prev_link = ""; } else { $prev_link = " <a href=\"http://Server_IP/cgi-bin/search.cgi/p +age$prev_page\">". "previous" . "</a>"; } if ($reqpage == $pagecount) { $next_link = ""; } else { $next_link = " <a href=\"http://Server_IP/cgi-bin/search.cgi/p +age$next_page\">". "next" . "</a>"; } if ($pagecount > 1) { $pagelinks = $prev_link; $pageno = 0; while ($pageno < $pagecount) { $pageno++; if ($pageno == $reqpage) { $thislink = " <strong>$pageno</strong> "; } else { $thislink = " <a href=\"http://Server_IP/cgi-bin/searc +h.cgi/page$pageno\">" . $pageno . "</a>"; } $pagelinks = $pagelinks . $thislink; } $pagelinks = $pagelinks . " " . $next_link; } else { $pagelinks = ""; } print "<br />"; print $count; print "<br /><br />"; print $pagelinks . "<br />"; print qq~ <br /> <br /> </body> </html>~; }

Any ideas on where I'm going wrong?
Thanks in advance.


In reply to Perl Database Paging by ironside

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.