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.


In reply to Re: Page Links... by december
in thread Page Links... by powerhouse

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.