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

时间:2010-09-14 09:32:46

标签: html perl cgi

以下是我从SQLite数据库中检索数据的表。 它有很多记录,所以附近的ADD按钮我需要类似的东西 |< < > >|只要我点击就会执行分页功能。 此外,除了表格每个标题(例如UserName UserId),我需要一个排序 按钮。像^按钮之类的东西。请帮我找到解决方案。谢谢。

  #!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

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

2 个答案:

答案 0 :(得分:1)

好的,首先,请阅读Learning Perl。这是最好的书,用来学习Perl。

接下来,请查看Ovid's CGI Course

第三,你的代码有一些重大问题,你需要在跑步前走路。

我已经整理并评论了你的代码。

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

#  Windows perl ignores the shebang, except to check for flags and 
#  arguments to start the Perl interpreter with.
#  Your webserver might use it though

# You forgot to enable strict.  You enabled warnings further down in
# your code.  These two pragmas will help you write bug free code by 
# catching many errors.
#
# Keep your module and pragma usage at the top of your 
# scripts.  It aids readability. 

use strict;
use warnings;

# Using CGI is a good idea, but you only need to use CGI one time.
use CGI qw/:all/;

# These are good while learning and debugging.
# Do not use them in production code.
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

use DBI;

my $dbh = DBI->connect(
    "dbi:SQLite:DEVICE.db",
    "", "",
    {
        RaiseError => 1,
        AutoCommit => 1
    }
);

# Don't use indirect object notation.  It can lead to subtle bugs.
# Use the arrow notation for method invocation instead.
my $q = CGI->new();
print $q->header ( );

# The @rows array was doing nothing.




# No need to commit when autocommit is on.
$dbh->commit();
$dbh->disconnect;

# Here we get the html table in a string.

my $table = generate_data_table( $dbi );

# And here we print your whole HTML block with the table interpolated
# into the the main text.  As it was, the HTML page was printing AFTER
# the table you generated.
#
# I put a crappy improper stylesheet in the header of your html page.
# Unless you are only doing the most rudimentary HTML work, learn to 
# use CSS properly.  Your time will be repayed hundreds of times over.
# For only rudimentary work, there's still a good chance you'll break 
# even on any time you invest in learning CSS.

print <<END_HTML;
<html>
<head>
    <title>Add Users</title>
    <style>
        .adduser {
            background-color:#CDC9C9;
        }
    </style>
</head>
<body>

    <form action="UsersList.cgi" method="get">

$table

    <input type="hidden" name="submit" value="Submit">
    </form>

</body>
</html>

END_HTML


# Use subroutines to group related actions.
sub generate_data_table {
    my $dbi = shift;

    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");

    # Actually generate the table HTML
    my $table = '<table><tr>';

    # Header
    $table .= join '', map "<th>$sth->{NAME}[$_]</th>\n", 0..5;
    $table .= "</tr>\n";

    # Normal Rows
    while (my @row = $sth->fetchrow_array) {
        $table .= '<tr>',
        $table .= join '', map "<td>$row[$_]</td>\n", 0..5;

        $table .= join "\n", 
            '<td><A HREF=\"\">EDIT</A></td>'
            '<td><A HREF=\"\">DELETE</A></td>'
            "</tr>\n";
    }

    # Special Row
    #
    # Don't use inline CSS, use classes and either group all your css at
    # the top of your html code, or better yet, load an external stylesheet.

    # There is no reason to have to escape quotes when working with Perl CGI.
    # First, in html ' and " are interchangeable, so you can pick a quote
    # that doesn't need esacaping.
    #
    # Finally, if you MUST use both ' and " in a single string, you can use
    # Perl's quoting operators (q and qq) to select a safe delimiter that will allow you
    # to avoid escaping.

    $table .= 
          "<tr class='adduser' >"
        . '<td><a HREF="http://localhost/cgi-bin/AddUser.cgi">ADD</a></td>'
        . '<td></td><td></td><td></td><td></td></tr>'
        . "</table>";

    $sth->finish();

    return $table;
}

最后,要处理排序和分页,您可以像其他人建议的那样使用库,或者您可以modify your SQL query。您只想获取一系列结果的关键字是LIMITOFFSET,请使用ORDER BY子句对结果集进行排序。在表单中添加一些参数,以指示您想要的排序方法或范围。

答案 1 :(得分:0)

使用DBIx::Class进行数据库访问可以获得的(许多)优势之一是所有搜索都内置了对分页的支持。

或者,您可能会发现Data::Page之类的内容非常有用。

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