2010-09-14 67 views
-2

這裏是我從SQLite數據庫中檢索數據的表。 它有很多的記錄,所以附近的ADD按鈕我需要類似 |< < > >|這將做尋呼功能,只要我點擊。 此外,除了每個表頭(例如用戶名UserId)我需要一個排序 按鈕。像一個^按鈕。請幫助我找到解決方案..謝謝。如何在Perl CGI程序中進行分頁和排序?

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

    ---------------------------------------- 
+0

請學會正確設置您的問題的格式。所有文本都被格式化爲代碼。這可能會讓一些人回答你的問題。 – 2010-09-14 09:36:48

+0

現在你似乎編輯了這篇文章來刪除你的實際問題。我們剩下的只是一些代碼。這不是我一直在建議的:-) – 2010-09-14 10:24:00

+4

它顯示了一個驚人的不尊重和輕蔑的人誰將幫助你,當你花很少的時間準備你的問題的基本拼寫,格式化,不介意更高層次的概念。爲什麼我們應該花時間回答你沒有足夠時間恭敬的問題? – 2010-09-14 11:02:26

回答

0

一個,你會使用DBIx::Class爲你的數據庫訪問得到(多)的優點是,所有的搜索都內置了分頁支持。

或者,您可能會發現像Data::Page這樣的內容有用。

至於排序,這可能是最好的SQL查詢與'排序'條款。

+0

我發現dbix class rar文件以及data :: page rar文件...我將它們下載到我的d盤中,但是當我嘗試使用分頁代碼時,我得到了錯誤...爲什麼這樣呢?我應該在哪裏保存rar文件..? – sonya 2010-09-14 10:57:12

+0

基類包「Class :: Accessor :: Chained :: Fast」爲空。 (也許你需要'使用'首先定義該軟件包的模塊, 或在@INC中使該模塊可用(@INC包含:C:/ Perl/site/lib C:/ Perl/lib。)。 at C:/Perl/site/lib/Data/Page.pm line 4 BEGIN失敗 - 編譯在C:/Perl/site/lib/Data/Page.pm第4行中止。 編譯在C語言中的require失敗:/ Program Files/Apache Software Foundation/Apache2.2/cgi-bin/UsersList.cgi第7行。 BEGIN失敗 - 編譯在C:\ Program Files/Apache Software Foundation/Apache2.2/cgi-bin/UsersList中止.cgi第7行。 – sonya 2010-09-14 10:58:30

+0

我已經在我的程序中包含了這段代碼..使用Data :: Page;我的$ page = Data :: Page-> new(); $ PAGE-> total_entries($ total_entries); $ PAGE-> entries_per_page($ entries_per_page); $ PAGE-> CURRENT_PAGE($ CURRENT_PAGE);打印「第一頁:」,$ page-> first_page,「\ n」;打印「最後一頁:」,$ page-> last_page,「\ n」;打印「頁面上的第一項:」,$ page-> first,「\ n」;打印「最後輸入頁面:」,$ page-> last,「\ n」; – sonya 2010-09-14 10:59:34

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子句對結果集進行排序。將一些參數添加到表單中以指示您想要的排序方法或範圍。

+0

非常感謝您爲我提供的所有時間和非常非常重要的信息,先生。感謝很多,是的,您是對的我需要有一個強大的基礎知識,然後再行動,再次感謝。 – sonya 2010-09-15 06:30:10

+0

任何人都可以告訴我,爲什麼我的信譽從6下降到1? – sonya 2010-09-15 09:11:41

+0

jene,RELAX!因此,我得到了很多帖子,比如「X怎麼寫?給我寫代碼!」。顯然,你已經設法讓人們把你放在這個班上。我不這樣看。對我來說,你的帖子問題似乎源於英文是你的第二語言(基於你的一些用法),並被SO和Perl,CGI以及HTML和CSS和SQL所淹沒。六件大事要一次學習。在任何情況下,SO似乎都會獎勵謹慎的正式寫作。你的代表會及時恢復。它很糟糕,但它提醒你注意一個問題。太糟糕了,你必須猜測它可能是什麼。 – daotoad 2010-09-15 15:41:56