Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Need help to improve pagination

by terrykhatri (Acolyte)
on Jul 21, 2014 at 15:14 UTC ( #1094521=perlquestion: print w/replies, xml ) Need Help??
terrykhatri has asked for the wisdom of the Perl Monks concerning the following question:

Hi Poj,

I have created pagination using the following code it works but does not look graceful as it displays links for all 103 pages what I need help for is to make it compact like : [First][Prev][1][2][3][4][5][6]...[103][Next][last], I have looked at perl pagination modules @ cpan but there aren't enough examples for a guy of my level to use them

# Count how many rows are there in a table, so that we can use it for +$pagenum. my $sql = qq!SELECT COUNT(*) from "Orders" !; my $sth = $dbh->prepare ("$sql"); $sth->execute() || quit(); my @row = $sth->fetchrow_array; $sth->finish; # Setting offset, limit and page number my $offset = 0; my $limit = 8; my $pagenum = ceil($row[0]/$limit); # Assigning value to $offset as 0 or whatever will be the $pagenum i.e +. 1 or 2 .... $offset=param('page')? $limit*param('page') :0; # Get the data $sql = qq!SELECT a."OrderID", b."CompanyName" AS "CustomerName", c."FirstName"::text || ' ' ||c."LastName"::text AS "E +mployeeName", a."OrderDate"::DATE, a."RequiredDate"::DATE, a."Shipp +edDate"::DATE, d."CompanyName" AS "ShipVia", a."Freight", a."ShipNam +e", a."ShipAddress", a."ShipCity", a."ShipRegion", a."ShipPostalCode", a."ShipCountry" FROM "Orders" a, "Customers" b, "Employees" c, "Shippers" +d WHERE a."CustomerID" = b."CustomerID" AND a."EmployeeID" = c."EmployeeID" AND a."ShipVia" = d."ShipperID" ORDER BY 1 LIMIT $limit OFFSET $offset !; $sth=$dbh->prepare("$sql"); $sth->execute() || quit(); .... then html stuff skiped.... # Showing page number with link my $first_page = $pagenum - $pagenum ; my $last_page = $pagenum - 1; $pagenum = $pagenum - 1; print q(<ul class="tsc_pagination tsc_paginationA tsc_paginationA09">) +; print qq(<li><a href='vieword.pl?page=$first_page'>First>); for my $i (0 .. $pagenum) { print qq(<li><a href='vieword.pl?page=$i'>$i>); } print qq(<li><a href='vieword.pl?page=$last_page'>Last>); print q(</ul>);

Your help as usual will be much appreciated.

Many thanks !

Terry

Replies are listed 'Best First'.
Re: Need help to improve pagination
by Laurent_R (Canon) on Jul 21, 2014 at 17:42 UTC
Re: Need help to improve pagination (stand up)
by Anonymous Monk on Jul 21, 2014 at 17:45 UTC
Re: Need help to improve pagination
by Anonymous Monk on Aug 08, 2014 at 00:04 UTC
Re: Need help to improve pagination
by Anonymous Monk on Jul 21, 2014 at 15:26 UTC
    Nice SQLi vector there, but you've been warned about this already.
Re: Need help to improve pagination
by Anonymous Monk on Jul 23, 2014 at 21:30 UTC

    perlintro#Perl variable types, Writing subroutines

    #!/usr/bin/perl -- ## ## ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { print join "\n", pageLinksHtml( 170, 11, 10, 5 ); } sub pageNumbers { return { first => 1 }, 9, 10, { curr => 11 }, 12, 13, { last => 17 }, ; } ## end sub pageNumbers sub pageLinksHtml { my( $total, $curr, $rate, $links ) = @_; my @nums = pageNumbers( $total, $curr, $rate, $links ); for my $num ( @nums ) { if( ref $num ) { if( my $first = $$num{first} ) { $num = sprintf '<li><a href="?;page=%d">{%d</a>...</li +>', $first, $first; } elsif( my $curr = $$num{curr} ) { $num = sprintf '<li><b>%d</b></li>', $curr; } elsif( my $last = $$num{last} ) { $num = sprintf '<li>...<a href="?;page=%d">%d}</a></li +>', $last, $last; } } else { $num = sprintf '<li><a href="?;page=%d">%d</a></li>', $num +, $num; } } return '<ul>', @nums, '</ul>'; } ## end sub pageLinksHtml
      Great, many many thanks, I will now try and figure out how to integrate your code with my script. Best regards. Terry

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1094521]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-02-24 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (121 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!