473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can I do the pagination in my Perl CGI script?

10
I have a CGI page with a table which is populated by fetching the data from database, in a word its like a DATAGRID.

And just at the bottom right end of tha Grid I need a link like "First << 1 2 >> Last"
or like" |< < > >| "on clicking which I can navigate to and fro the records. And I am intend to have "10" records per page.

While surfing I got a piece of code which I am going to paste in the code field. But problem is that it displays the paging link something like this "1 2 3 4 5 ..and so on". But I am not willing to have this paging format because the number of records increases the length of the link also goes on increasing. So can this code be modified to the format which I am intending to have?

Expand|Select|Wrap|Line Numbers
  1.      #!C:\perl\bin\perl.exe
  2.  
  3.        use CGI::Carp qw(fatalsToBrowser);
  4.        use DBI;
  5.        use CGI qw/:standard/;
  6.        use CGI;
  7.        use POSIX;
  8.        print "Content-Type: text/html\n\n";
  9.        $|++;
  10.  
  11.        my $env=$ENV{QUERY_STRING};
  12.        my@env=split(/=/, $env);
  13.        my $page= $env[1];
  14.        my ($results_per_page);
  15.        if (!$limit) {
  16.           $limit = 0;
  17.            }
  18.        my $dbh = DBI->connect(
  19.             "dbi:SQLite:COMPANY.db",
  20.             "", "",
  21.             {
  22.                 RaiseError => 1,
  23.                 AutoCommit => 1
  24.             }
  25.         );
  26.  
  27.         my $query =$dbh->selectall_arrayref('SELECT * 
  28.         FROM Employees');
  29.         my $count=@$query ; 
  30.         $results_per_page = 3;
  31.         my $pagesrequired = ceil($count  / 
  32.                $results_per_page);  
  33.  
  34.        [... PIECE OF CODE : 1 WHICH I WANT TO GET 
  35.          REPLACED BY CODE:2 MENTIONED BELOW
  36.              THIS CODE ,
  37.          for (my $i = 0; $i <= $pagesrequired -1; $i++) {
  38.          if ($i == 0) {
  39.            if ($limit == 0) {
  40.               print "<a href=\"Employees.cgi?&limit=0
  41.                \">";
  42.               print $i + 1;
  43.               print "</a>";
  44.              }
  45.            else {
  46.                print $i + 1;
  47.              }
  48.             }
  49.  
  50.           if ($i > 0) {
  51.           if ($limit != ($i * $results_per_page)) {
  52.             print " | <a href=\"Employees.cgi?&limit=";
  53.             print ($i * $results_per_page);
  54.             print "\">\n";
  55.             print $i + 1,"</a>";
  56.              }
  57.           else {
  58.             print " | ", $i + 1;
  59.              }
  60.            }
  61.           }       ...]
  62.           THIS CODE ENCLOSED BETWEEN [.. ..]GIVES THE
  63.           PAGINATION FORMAT AS "1 2 3 4.SO ON".
  64.           BUT I NEED TO GET THE FORMAT OF |< < > >| AS 
  65.           THIS CODE BELOW GENERATES...I AM 
  66.           NOT ABLE TO REPLACE THIS CODE WITH THAT OF THE
  67.           ABOVE ONE, PLEASE HELP ME OUT.
  68.  
  69.          [...CODE :2
  70.               WHERE DECLARATONS FOR BELOW ARE AS 
  71.           FOLLOWS...
  72.                 my $pageNum =$query->param('pageNum'); 
  73.                 unless($pageNum) {
  74.                      $pageNum=1;
  75.                    } 
  76.                 my $offset=$query->param('offset');
  77.                         unless($offset) {
  78.                         $offset=3;
  79.                     }
  80.               AND 
  81.                 my $dbh = DBI->connect
  82.                 ("dbi:SQLite:DEVICE.db","", "",
  83.                 {
  84.                 RaiseError => 1,
  85.                 AutoCommit => 1
  86.                  }
  87.                  );
  88.                  my $query =$dbh->selectall_arrayref
  89.                   ('SELECT * FROM UsersList');
  90.                  my $numOfRec=@$query ;
  91.  
  92.                  my $numofPages = ceil($numOfRec / 
  93.                   $offset);  
  94.  
  95.                  /*HERE'S THE CODE WHICH I WANT TO 
  96.                   REPLACE IN PLACE OF CODE:1*/
  97.               if ( $pageNum > 0 ) {
  98.              print q[<td><a href="Employees.cgi?pageNum=1
  99.                ">|&lt;</a></td>];
  100.              $pageN  = $pageNum - 1;
  101.              print qq[<td><a href="Employees.cgi?
  102.               pageNum=$pageN">&lt;</a></td>];
  103.                 }
  104.             else  {
  105.                   print q[<td><span
  106.               class="currentpage">|&lt;</span></td>];
  107.                   print q[<td><span
  108.               class="currentpage">&lt;</span></td>];
  109.                   }
  110.  
  111.             if ( $pageNum < ( $numofPages - 1 )) {
  112.                  $pageN  = $pageNum + 1;
  113.                  print qq[<td><a href="Employees.cgi?
  114.                    pageNum=$pageN">&gt;</a></td>];
  115.                  print qq[<td><a href="Employees.cgi?
  116.                   pageNum=$numofPages">&gt;|</a></td>];
  117.                 } 
  118.              else {
  119.             print q[<td><span
  120.                 class="currentpage">&gt;</span></td>];
  121.             print q[<td><span 
  122.                 class="currentpage">&gt;|</span></td>];
  123.                 }    ...]  THE END...
  124.  
  125.              /*REST OF THE CODE AND AN ERROR WHICH I 
  126.               GONA MENTION BELOW AT THAT PARTICULAR 
  127.               LINE */
  128.  
  129.               my $sth = $dbh->prepare("SELECT
  130.     EmployeeId,EmployeeName,Address,Company,Role,
  131.     Branch FROM Employees 
  132.                   limit ?,? ");
  133.              $sth->bind_param(1, undef,SQL_NUMERIC);
  134.              $sth->bind_param(2, undef,SQL_NUMERIC);
  135.            Line:64  $sth->execute
  136.                   ($page,$results_per_page); 
  137.           /* ERROR:DBD::SQLite::st execute 
  138.              failed: datatype mismatch at C:/Program
  139.              Files/Apache SoftwareFoundation/Apache2.2
  140.               /cgi-bin/Employee.cgi line 64.*/
  141.  
  142.               print start_html('Employee LIST'),
  143.               table({-border=>'1',width=>'100%'}),
  144.               Tr({-align=>'CENTER', -valign=>'TOP'},
  145.               [
  146.               th(["EmployeeId"
  147.                 ,"EmployeeName","Address","Company","Role
  148.                   ","Branch "] )
  149.               ]);
  150.              while (my ($EmployeeId
  151.       ,$EmployeeName,$Address,$Company,$Role,$Branch ) = 
  152.                    $sth->fetchrow_array()) {
  153.                     if ($UserName eq "") {
  154.                                $UserName="UNDEF";
  155.                                           }
  156.          print Tr ({-align=>'CENTER',},
  157.                 [
  158.                 td([$EmployeeId
  159.         ,$EmployeeName,$Address,$Company,$Role,
  160.               $Branch ]),
  161.                 ]);
  162.               }
Sep 17 '10 #1
1 4954
RonB
589 Expert Mod 512MB
Take a look at Data::Page and its cousins.
http://search.cpan.org/search?query=data+page&mode=all
Sep 17 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
6
by: Richard Trahan | last post by:
I want a js function to call a Perl script residing on a server. The Perl script will return a string, to be used by the js. Pseudo code: <script> stringvar = perlfunc_on_server(stringarg)...
4
by: benwylie | last post by:
I am running IIS 6.0 on Windows 2003. I would like to be able to run a perl script from a web page and include the output. I have tried doing it with an ssi: <form action='docsearch.shtml'...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
2
by: kimonp | last post by:
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki. I am trying to call a perl function from within php using proc_open. The perl script is being executed and...
2
by: IvanIV | last post by:
Hi All, I have trouble with embedding a Perl interpreter into a C program. I would like to replace some subroutine in a Perl script without changing this script from my C program. I need...
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
3
by: happyse27 | last post by:
Hi All, I am creating the perl script using html form(with embedded javascript inside). When using this html form with javascript alone, it works where the form validation will pop up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.