Moryadesign's Blog

Pagination in Perl

Posted by: moryadesigns on: September 5, 2009

WordLinx - Buy Real Visitors

Pagination is a very basic requirement of a web application, in this article we’ll see how to implement pagination easily using the Perl module Data::Pageset.

Basically there are two types of pagination, Jumping and Sliding. In the jumping type the current page jumps from the starting position in a frame till the end and goes till the get before moving on to the next frame. Whereas, in the sliding type the current page remains in the center of the frame (of course except the current page is not in the starting or ennding half of the pageset), here is an illustration of the jumping and sliding pagination types which will help you understand the logic better.

Jumping

Code:
   [1] 2  3  4  5  =>   # first frame: [1-5]
<=  1 [2] 3  4  5  =>
<=  1  2 [3] 4  5  =>
<=  1  2  3 [4] 5  =>
<=  1  2  3  4 [5] =>   # current page jumps to the next frame
<= [6] 7  8  9 10  =>   # second frame: [6-10]
<=  6 [7] 8  9 10  =>
<=  6  7 [8] 9 10  =>

Sliding

Code:
  [1] 2  3  4  5  =>
<= 1 [2] 3  4  5  =>
<= 1  2 [3] 4  5  =>   # from here onwards current page remaing in the center
<= 2  3 [4] 5  6  =>   # current page
<= 3  4 [5] 6  7  =>   # and it stays there...
<= 4  5 [6] 7  8  =>
<= 5  6 [7] 8  9  =>
<= 6  7 [8] 9 10  =>

The Code

## the default is Jumping
use Data::Pageset;

my $total_entries = 500;
my $entries_per_page = 10;
my $current_page = 30;
my $pages_per_set = 11;

my $paging_jump = Data::Pageset->new({
    'total_entries'       => $total_entries,
    'entries_per_page'    => $entries_per_page,
    # Optional, will use defaults otherwise.
    'current_page'        => $current_page,
    'pages_per_set'       => $pages_per_set
});

my $paging_slide = Data::Pageset->new({
    'total_entries'       => $total_entries,
    'entries_per_page'    => $entries_per_page,
    # Optional, will use defaults otherwise.
    'current_page'        => $current_page,
    'pages_per_set'       => $pages_per_set,
    'mode'                => 'slide'
});

# Print the page numbers of the current set : Jumping
foreach my $page (@{$paging->pages_in_set()})
{
    if($page == $paging_jump->current_page())
    {
        print "[$page] ";
    }
    else
    {
        print "$page ";
    }
}

print "\n\n";

# Print the page numbers of the current set : Jumping
foreach my $page (@{$paging_slide->pages_in_set()})
{
    if($page == $paging->current_page())
    {
        print "[$page] ";
    }
    else
    {
        print "$page ";
    }
}

2 Responses to "Pagination in Perl"

Hi,

thanks for the great quality of your website, every time i come here, i’m amazed.

I would like to suggest you to come and try the true black hattitude.
you’ll find a lot of stuff dealing with the black hattitude,

You can buy some black hattitude, rent black hattitude, steal black hattitude, or find
the ultimate black hattitude on our site ofblack hattitude.

have a nice day,

Mike Litoris

black hattitude

you’ll find here also some good black hattitude

Thanks dear

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

MoryaDesigns

MoryaDesigns

……..A complete dynamic scripting solution

want your website to be most beautiful,eye-catching and user friendly website

just contact us
moryadesigns@gmail.com

Archives

Follow

Get every new post delivered to your Inbox.