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

Hello Monks, I'm trying to modify some code to let me have multiple page listings using a previous page, page 1, page 2, next page feature. So far, I was able to successfully modify the code to show the requested number of hits on a page. And, I do get the correct number of hits. My problem is that the navigation link to show previous page, page 1, page 2, next page is not appearing on the bottom of the page. I am not sure what is wrong. Can someone take a look at my code and give me a hand? The line "print hr(), @nav_link should print the navigation links on the page but it doesn't.
sub page_parameters { my ($dbh, $where, @placeholder) = @_; my ($start_pos, $page_size, $max_rec); my @nav_link; # navigation link array my $limit; # LIMIT clause my @row; # summary table row array my ($stmt, $sth); my $str; my $page; # Get the page control parameters. If they're not present, this i +s # the first time we're running this search. In that case, run a q +uery # to determine the result set size and initialize the page paramet +ers. #@ INIT_PAGE_PARAMS $start_pos = param ("start_pos"); $page_size = param ("page_size"); $max_rec = param ("max_rec"); if (!defined (param ("start_pos"))) { $start_pos = 0; $page_size = 2; # change this to change #hits/page $stmt = "SELECT COUNT(*) FROM catalog_pet $where"; $max_rec = $dbh->selectrow_array ($stmt, undef, @placeholder); if ($max_rec == 0) { print p ("Sorry, no qualifying listings were found."); return; } # put values into environment so gen_nav_link() can find them # (except for start_pos, which isn't constant across links) param (-name => "page_size", -value => $page_size); param (-name => "max_rec", -value => $max_rec); } #@ INIT_PAGE_PARAMS # $start_pos = number of initial records to skip # $page_size = number of records to retrieve $limit = "LIMIT $start_pos, $page_size"; print p ("$max_rec matching listings were found."); $sth = $dbh->prepare ("SELECT * FROM catalog_pet $where $limit"); $sth->execute (@placeholder); #@ BUILD_NAV_LINKS if ($max_rec > $page_size) { #@ PREV_PAGE_LINK if ($start_pos == 0) # first page: no prede +cessor { push (@nav_link, "previous"); } else { push (@nav_link, gen_nav_link ("previous", $start_pos-$pag +e_size)); } #@ PREV_PAGE_LINK for (my $i = 0; $i < $max_rec; $i += $page_size) { my $page_no = int ($i / $page_size) + 1; if ($start_pos == $i) # this is the current + page { push (@nav_link, $page_no); } else { push (@nav_link, gen_nav_link ($page_no, $i)); } } if ($start_pos+$page_size >= $max_rec) # last page: no succ +essor { push (@nav_link, "next"); } else { push (@nav_link, gen_nav_link ("next", $start_pos+$page_si +ze)); } my $table = get_product_table ($sth); $page .= ($table ? $table : p ("No items were found.")); $sth->finish (); return ($page); @nav_link = map { "[$_]\n" } @nav_link; print hr(), @nav_link; } #@ BUILD_NAV_LINKS }
sub gen_nav_link { my ($label, $start_pos) = @_; my @param = # parameters to extract from environment ( # page control parameters "max_rec", "page_size", # search parameters "description" ); my $url; # tell the script to continue the search and which record to start + with $url = url () . "?choice=search;start_pos=$start_pos"; # add other page control and search parameters foreach my $name (@param) { my @val = param ($name); # if a parameter has multiple values, add it multiple times foreach my $val (@val) { $url .= ";$name=" . escape ($val); } } return (a ({-href => $url}, escapeHTML ($label))); }
Thanks for your help.

Replies are listed 'Best First'.
Re: Multiple page listings - previous/next feature
by poj (Abbot) on Feb 07, 2003 at 19:08 UTC
    #return ($page); ### this line is too early @nav_link = map { "[$_]\n" } @nav_link; print hr(), @nav_link; return ($page); ### put it here
    poj
      Hi, I made the modification based on your suggestion. I'm receiving a 500 Internal Server Error. Any thoughts? Thanks
        Did you try to run the program from the command line to see if you can figure out where your error(s) are? Make sure you use carp (if you already are not doing so), as in:
        use CGI::Carp qw(fatalsToBrowser);
        This should at least help you troubleshoot without being blind to the trouble. Keep at it, you are almost there.
        It must be the print statement that is now being executed. I can't explain the error but if you want the links at the bottom of the page you can't print them inside the sub. I suggest you add them onto the page returned like this ;
        @nav_link = map { "[$_]\n" } @nav_link; $page .= "<hr>@nav_link"; return ($page);
        poj
Re: Multiple page listings - previous/next feature
by cees (Curate) on Feb 07, 2003 at 21:22 UTC

    Have you had a look at the HTML::Pager module? It does pretty much what you are doing here.

      Hi. No, I have not looked at HTML::Pager. I check it out and see how it works.