1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Easy Pagination In Perl

Discussion in 'Perl' started by pradeep, Jul 3, 2009.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:

    Introduction



    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



    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 ";
    	}
    }
    
    Hope you liked the article and find it useful, you can check more about the module at http://search.cpan.org/~llap/Data-Pageset-1.05/lib/Data/Pageset.pm
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83

Share This Page