ironside has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to write a script to display database results on several pages with links. I can display the first page of results with the correct number of links. The problem is that the links don't show the next page of the results. When clicked, nothing happens.

I'm thinking the problem is with the links. I am having a hard wrapping my brain on how to create these links properly and what exactly I should be putting in them. I'm sure I am close to getting this working, so any help would be much appreciated.

Thanks in advance.

Here is my script:

#!/usr/bin/perl use CGI qw(:standard); use DBI; use warnings; use strict; my (@row,$name,$path,$dsp,$count); my $query = new CGI; print $query->header('text/html'); print qq~ <!DOCTYPE html> <html> <head> <title>Movie Search</title> </head> <body> <br /><br />~; my $reqpage = $query->param('reqpage') || '1'; param('per_page'); #Get total amount of rows from db my $dbh=DBI->connect($connectionInfo,$user,$passwd); my $num_rows= $dbh->selectrow_array('select count(name) from video + order by name'); my $num_results_perpage = 25; # calculate the number of pages to show my $pagecount = int($num_rows / $num_results_perpage); if (($pagecount * $num_results_perpage) != $num_rows) { $pagecount++; } # calculate which results to show in the page my $firstresult = (($reqpage - 1) * $num_results_perpage) + 1; my $lastresult = $firstresult + $num_results_perpage - 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_perpage"); $sth->execute(); while (@row = $sth->fetchrow_array()) { $name = $row[0]; $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://path/to/script.cgi?reqpage=$re +qpage&page=$prev_page\">". "previous" . "</a>"; } if ($reqpage == $pagecount) { $next_link = ""; } else { $next_link = " <a href=\"http://path/to/script.cgi?reqpage=$re +qpage&page=$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://path/to/script.cgi?reqp +age=$reqpage&page=$pageno\">" . $pageno . "</a>"; } $pagelinks = $pagelinks . $thislink; } $pagelinks = $pagelinks . " " . $next_link; } else { $pagelinks = ""; } print "<br />"; print $count . "<br /><br />"; print $pagelinks . "<br />"; print "Database Results: " . $num_rows . "<br />"; print "Page " . $firstresult . " Of&nbsp;&nbsp;" . $pageno; print qq~ <br /> <br /> </body> </html>~;

Replies are listed 'Best First'.
Re: pagination links (logic)
by Anonymous Monk on Apr 02, 2014 at 23:59 UTC
    This is how you should write that
    #!/usr/bin/perl -- use strict; use warnings; use CGI (); Main( @ARGV ); exit( 0 ); sub Main { my $q = CGI->new; ... my @currentPageData = GetItGood( ... ); ... return PaginationFudge( $numberResults, $numperPerPage, $currentPageNumber, \@currentPageData, ); }

    Then sub PaginationFudge prints the html to what it should be .... subs! DebugCGI

    Logic is the same in every language, even perl

      Thank you but that doesn't solve my problem. I removed the subs that I had to simplify the script until I can figure out how to get paging working.

        Thank you but that doesn't solve my problem.

        It wasn't meant to solve your problem (it wasn't an answer) -- its a way to organize your code so that solving the problem is easier than easy ... its about a million times easier to debug HtmlPrinter() or PaginationLinks() than HtmlPrinterAndDatabaseQueryAndCGIQueryAndPaginationLogicAllWhileEscapingQuotes()

        You want to debug the smallest piece possible, not the whole thing at once in a blender ; Its how you can solve your own problem and makes it a joy for others to help you

        I removed the subs that I had to simplify the script until I can figure out how to get paging working.

        You will never figure it out that way ... and its very painful for folks trying to help you ...

        Its kind of like building a bed out of wood by only using a drill -- how are you cutting your lumber, using saw? No, I'm drilling it ... very slow going and painful ...

        Perl gives subs, gives you double quoted strings where you don't have to escape quotes ... it gives you all manner of tools besides drill

Re: perl pagination links
by poj (Abbot) on Apr 03, 2014 at 19:04 UTC

    Consider simplifying the page link code to something like this

    # page links my $pagelinks; if ($reqpage > 1){ my $prev_page = $reqpage - 1; $pagelinks .= qq! <a href="?reqpage=$prev_page"> Previous </a> !; } for my $pageno (1..$pagecount){ if ($pageno == $reqpage){ $pagelinks .= qq! <strong>$pageno</strong>!; } else { $pagelinks .= qq! <a href="?reqpage=$pageno"> $pageno </a> !; } } if ($reqpage < $pagecount){ my $next_page = $reqpage + 1; $pagelinks .= qq! <a href="?reqpage=$next_page"> Next </a> !; }
    poj

      poj you're absolutely brilliant! That did the trick. I did a lot of testing and I had a feeling that's where the problem was.

      Thank you for taking the time to help. Perl Monks are top class.
      Cheers poj!