We've used a brute force method similar to this in a recent
project:
- Pass (in a hidden field) the CurrentPage Number for each
display
- Generate the entire list of "results"
- Take a slice from the list to just send the correct
entries
This is simple to code, and makes it easier to let the user
jump straight to a particular page, and to choose how many
results to see on each page (the bit of code below doesn't
do either of these).
my $CurPage = CGI::param('CurrentPage');
if (defined CGI::param('NextPage')){
$CurPage++;
} elsif (defined CGI::param('PrevPage')){
$CurPage--;
}
print qq{
<input type="hidden" name="CurrentPage" value="$CurPage">
};
for my $Entry (@Entries[$CurPage * 5 .. $CurPage * 5 + 4]){
# Show each entry however you normally would
}
print '<a href="myscript.cgi?Prev=1">Previous</a>' if $CurPage;
print '<a href="myscript.cgi?Next=1">Next</a>'
unless $CurPage * 5 + 5 >= @Entries;
Some notes:
- The slice line looks a little goofy (adding 4 when we
want 5 per page), I could write it as "+5 - 1" (and
when we have a variable number of results, we do) but
whatever...
- This is pretty wasteful (we do the entire database
lookup for each page, then throw away what we don't
want)...
Hope this helps a little...
Russ | [reply] [d/l] |
How are you storing the data in your script? I assume
you have some sort of data structure? If you're storing
the data in an array, just print out a next link with a
parameter like "start=5"; then in your script, just print
out the array elements from the start value (5, in this
case) to the limit of records per page (the limit, in
your case, would seem to be 5).
If you're not storing the data in a data structure, but are
instead printing out each line in a while loop as you
read from the file, you can check $. for the current line
number, and only print the line if the line number is
between 5 and 10. Or something such. You could last
out of the loop after you hit the limit.
Does this make sense? There's no magic involved, really--you
just have to code it up yourself.
Also, check out Randal's WebTechniques column on this:
Giving 'more'. | [reply] |
Look on CPAN for HTML::Pager - It was designed to do just this.
| [reply] |
| [reply] |
The principal solution is to export the number of the last entry through post, i.e.: a href="myscript.cgi?entry=5"
Then you can use the following routine in your script:
if ($entry >=5) {my $backnum= $entry-5} else {my $backnum=0};
my $nextnum=$entry+5;
while ($entry % 5) {
#do your output stuff ;
$entry++;
if ($backnum) {
print "<a href=\"myscript.cgi?$backnum\">Previous</
+a>"};
};
print "<a href=\"myscript.cgi?$nextnum\">Next</a>"};
#other stuff...
However, if you would not use CGI module at all, you may parse parameters as $ARGV[0]
You might make a check for the nnumber of entries not to make your Next link to the enmpy page.
--
With best regards
Maqs. | [reply] [d/l] |
Not to be a noodge, but isn't that get that pases data through the URL, not post?
Either way, I'm all about using argv[0] for this sort of thing-- it works great for situations where you need to pass data but don't really need the functionality of a form, and I tend to botch it much less frequently than I botch get and post.
-T.McWee
The Autonomic Pilot
| [reply] [d/l] |