ironside has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Database Paging
by graff (Chancellor) on Mar 18, 2014 at 02:31 UTC | |
by ironside (Acolyte) on Apr 02, 2014 at 23:31 UTC | |
by graff (Chancellor) on Apr 03, 2014 at 01:44 UTC | |
by ironside (Acolyte) on Apr 03, 2014 at 22:43 UTC | |
by ironside (Acolyte) on Apr 03, 2014 at 04:23 UTC | |
|
Re: Perl Database Paging
by howdoyouperl (Initiate) on Jun 15, 2016 at 16:06 UTC | |
by Anonymous Monk on Apr 28, 2017 at 14:14 UTC |