Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Page Links...

by december (Pilgrim)
on Mar 02, 2003 at 17:09 UTC ( [id://239867]=note: print w/replies, xml ) Need Help??


in reply to Page Links...

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://239867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-25 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found