我如何在Perl CGI程序中进行分页和排序?

问题描述:

这是我从SQLite数据库中检索数据的表。
它有很多记录,所以接近ADD按钮我需要类似
|< &LT; &GT; > | 只要我点击,它就会执行分页功能。
此外,除了每个表头(例如用户名UserId)我需要一个排序
按钮。就像 ^ 按钮。请帮助我找到解决方案..谢谢。

Here is the table in which I am retrieving the data from an SQLite database. Its having lots of records, so near that ADD button I need something like |< < > >| which would do the paging function whenever I click. Also, besides the table each header (e.g. UserName UserId) I need a sorting button. Something like a ^ button. Please do help me find the solution..Thank You.

  #!C:\perl\bin\perl.exe

  use CGI;
  use CGI qw/:standard/;
  use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  my $q = new CGI;
  use DBI;
  use CGI qw(:all);
  use warnings;
  print $q->header ( );
  my $dbh = DBI->connect(
        "dbi:SQLite:DEVICE.db",
        "", "",
        {
            RaiseError => 1,
            AutoCommit => 1
        }
    );
  my @rows = ();
  my $sql = "SELECT UserId,UserName,CardNo,GroupId,Role,VerifyType FROM UsersList";
  my $sth = $dbh->prepare($sql) or die("\n\nPREPARE ERROR:\n\n$DBI::errstr");
  $sth->execute or die("\n\nQUERY ERROR:\n\n$DBI::errstr");
  print '<table>';
  print "<tr>";
  print "<th>$sth->{NAME}->[0]</th>";
  print "<th>$sth->{NAME}->[1]</th>";
  print "<th>$sth->{NAME}->[2]</th>";
  print "<th>$sth->{NAME}->[3]</th>";
  print "<th>$sth->{NAME}->[4]</th>";
  print "<th>$sth->{NAME}->[5]</th>";
  print "<th>  EDIT  </th>";
  print "<th>  DELETE  </th>";

  while (my @row = $sth->fetchrow_array) {
  print "
  <tr>
  <td>$row[0]</td>
  <td>$row[1]</td>
  <td>$row[2]</td>
  <td>$row[3]</td>
  <td>$row[4]</td>
  <td>$row[5]</td>
  <td><A HREF=\"\">EDIT</A></td>
  <td><A HREF=\"\">DELETE</A></td>
  </tr>";
   }
  print "<tr style='background-color:#CDC9C9;'><td><A HREF=\"http://localhost/cgi- 
     bin/AddUser.cgi\">ADD</A></td><td></td><td></td><td></td><td></td></tr>";
  print"</table>";
  $sth->finish();
  $dbh->commit();
  $dbh->disconnect;

  print <<END_HTML;
  <html>
  <head><title></title></head>
  <body>
  <form action="UsersList.cgi" method="get">
  <TABLE align="center">
  <TR>
  <TD align="left">
  <input type="hidden" name="submit" value="Submit">
  </TD>
  </TR>
  </TABLE>
  </form>
  </body></html>
  END_HTML

  ----------------------------------------


您可以使用 DBIx :: Class 来访问您的数据库,搜索内置了对分页的支持。

One of the (many) advantages that you'd get from using DBIx::Class for your database access is that all searches have built-in support for paging.

或者,您可能会发现类似于 Data :: Page 是有用的。

Alternatively, you might find something like Data::Page to be useful.

至于排序,这可能是最好的SQL查询'sort'子句。

As for sorting, that's probably best done in your SQL query with a 'sort' clause.