Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Page Links...

by powerhouse (Friar)
on Mar 02, 2003 at 05:35 UTC ( #239825=perlquestion: print w/replies, xml ) Need Help??
powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I am using a MySQL database and counting the rows in the database.

$count now contains the number of records in the database.
Can someone show me a sample of how I would go about creating links to each page, if $start_limit contains the starting number. For example, $count = 200; and $start_limit = 20; $start_limit is the number of records to show on the page.

How can I get a page navigation system out of that? I keep confusing myself, so I need some help. I would really appreciate it. I want it to put the number of pages, for instance with the numbers above it would do something like this:

1 2 3 4 5 6

Where it will NOT put a link for the page it is on. I have been trying various things for about 2 hours now and am getting so frustrated. I'd really appreciate some advice on modules or something that already do this, or just a simple way to do it.

thx,
Richard

Replies are listed 'Best First'.
Re: Page Links...
by tachyon (Chancellor) on Mar 02, 2003 at 07:31 UTC

    This should get you going... I have shown you how to do page ranges just for the hell of it.

    # http://somesite.com/cgi-bin/display.pl?current_page=12 # display.pl #!/usr/bin/perl -w use strict; use CGI; #use DBI; my $SCRIPT = 'http://somesite.com/cgi-bin/display.pl'; my $RECS_PER_PAGE = 10; my $q = new CGI; print $q->header; #my $dbh = DBI->connect( etc.... ); if ( $q->param('start')) { display_this_db_section( $q->param('start'), $q->param('end'), $q- +>param('current_page') ); } else { my $count = get_record_count(); my $page = $q->param('current_page') || 1; my $html = get_current_page($page); $html .= generate_links($count, $page ); print $html; } exit 0; #### SUBS #### sub display_this_db_section { my ($start, $end, $current_page ) = @_; $current_page ||= $start; my $count = get_record_count; die_nice ( 'Invalid' ) if $start > $count or $start < 1 or $end > $count or $end < 1 or $start > $end or $start > $current_page or $end < $current_page; print "<h3>Here is $current_page of $start-$end<h3>\n"; print "<p>", generate_links( $count, $current_page ) # blah } sub generate_links { my ($count, $current_page ) = @_; my $html = ''; for ( my $start = 1; $start <= $count; $start += $RECS_PER_PAGE ) +{ my $end = $start + $RECS_PER_PAGE -1; $end = $count if $end > $count; $html .= ( $start <= $current_page and $current_page <= $end ) + ? "[$start-$end] " : qq!<a href="$SCRIPT?start=$start&end=$end">[$start-$e +nd]</a> \n!; } return $html; } sub get_current_page { my $page = shift; # validate page, get data, format, return html return "<p>This is the content for page $page<p>\n"; } sub get_record_count { # get count of records, simulate here return 42; } sub die_nice { print shift and exit }

    Prints

    This is the content for page 1

    [1-10] [11-20] [21-30] [31-40] [41-42]

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Page Links...
by domm (Chaplain) on Mar 02, 2003 at 12:04 UTC
    There are also some Modules on CPAN which might work for you, depending on your setup and templating system (you are using one, are you?)

    -- #!/usr/bin/perl for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
Re: Page Links...
by OM_Zen (Scribe) on Mar 02, 2003 at 07:01 UTC
    Hi ,

    The theortical approach would be by :

    First get the rows twenty ($start_limit) starting from a variable that is supplied by a flag when it is the first page or a hidden parameter that can be supplied by the submission of next button at the end of the page for all the other pages .

    The page is count incremented for this set
    # Just a sample to indicate the flow (untested) my $pages = $cgi->param{'pages'}||1; my $start_limit = shift|| $cgi->param {'start_limit'}; # Get the record info starting from the # $start_limit record and the twenty from it # print "the records \n"; # print "the $pages at the end of the page "; $start_limit += $start_limit; $pages += $pages; # ALSO pass $start_limit and $pages as the hidden paramete +rs to the page occuring next to this


    This is just to give an idea of what should be a navigation scheme through maintaining hidden parameters between pages and also the part of select to get the rows within a range which is $start_limit to $start_limit + 20 is done by rownum in oracle and if that is different process it in the perl with $start_limit parameter .The present application which you have written should be posted so that any constructive suggestions could be discussed . Also go through the CGI::FormBuilder API from the CPAN that shall give you some idea to maintain state too among pages , these are just a few suggestions
Re: Page Links...
by december (Pilgrim) on Mar 02, 2003 at 17:09 UTC

    Quick and dirty example. It takes a parameter 'current', that keeps track which page you're looking at (for now, a regular variable so you can test the script in a shell).

    I used an array instead of a database because I don't have any databases running here, but the point should be the same. Just skip the initialization part, and don't forget to add input checks!

    #! /usr/bin/perl -Tw # # -wvh- quick and dirty pager example # use strict; my ($i, $current, $start_limit, $count); my (@hello, %param); # initiate variables (you use db code) $count = 200; $start_limit = 20; $param{'current'} = 46; # <-- test here to simulate page views # initiate array with page contents for ($i = 0; $i <= $count; $i++) { $hello[$i] = "This is record $i"; } ##### cut here ##### $current = $param{'current'}; # <-- do input checks here! $current = 0 if (! $param{'current'}); # optional: round to closest number if input number has been manually # changed to a number not divisable by $start_limit if ($current % $start_limit != 0 && $current != 0) { $current -= $current % $start_limit; } print "$count matches found for your query: \n"; # print pager for ($i = 0; $i <= $count; $i += $start_limit) { if ($i != $current) { print "<a href=\"query.cgi?current=$i\">$i</a>"; } else { print $i; } print " | " if ($i <= ($count - $start_limit)); } print "\n"; # print records for current page for ($i = $current; $i < ($current + $start_limit); $i++) { print $hello[$i] . "\n" if ($i <= $count); }

    Feel free to ask questions or point out mistakes. ;)

    Modified a small error with the pager seperators if the number of results wasn't divisable by start_limit.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://239825]
Approved by mjeaton
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2018-10-15 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (82 votes). Check out past polls.

    Notices?