tags:

views:

106

answers:

2

Hello everybody i hope everybody is doin well, Actually i have a table in which i fetch the data from sqlite database,i have done the paging and filtering for the grid using perl,now i need to do the sorting. The way in which i want to do is "MAKE THE TABLE HEADERS AS HYPERLINK AND WHENEVER I CLICK THEM THEN IT SHOULD SORT THE TABLE IN ASCENDING OR DESCENDING "ORDER BY" THE COLUMN WHICH I CLICK. Please do let me know is it possible, if yes then please do guide me find the solution. HERE'S My Code. Thank You.

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

use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use warnings;
use DBI;
use POSIX;
my $query = new CGI;
my $q = new CGI;
my $numofPages;
my $i;
my @overduedata;
my $pageN;
my @rows = ();
my $pageNum=$query->param('pageNum');
my $Id="";
my $Name="";
my @list;
my $var;
my $val;
my $filterexpression= " ";

print "Content-Type: text/html\n\n";
$query = $ENV{'QUERY_STRING'}; 
@list = split( /\&/, $query);  
 foreach (@list) {
  ($var, $val) = split(/=/);
  $val =~ s/\'//g;
  $val =~ s/\+/ /g;  
  $val =~ s/%(\w\w)/sprintf("%c", hex($1))/ge; 
  ($var, ' = ', $val,); 
 }  

 print <<END_HTML;
 <html>
 <head><title></title>
 </head>
 <body>
 <form action="Filtering.cgi" method="post">
 <TABLE>
  <TR>
   <TD>
     <input type="hidden" name="submit" value="Submit">
   </TD>
 </TR>
 </TABLE
 </form>
 </body></html>
 END_HTML


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

  my $offset;
   unless($offset) {
   $offset=11;
   }


 if ( $q->param("Filter") ) 
  {
   my $Id=$q->param('User_Id');
   my $Name=$q->param('User_Name');
  if ($Id ne "" )
      {
    $filterexpression= $filterexpression." UserId like '" .$Id. "%' and " ;
       }

  if ($Name ne "" )
  {
   $filterexpression= $filterexpression." UserName like '" .$Name. "%' and " ;
   }
   }
 $filterexpression= $filterexpression. " UserId > 0"  ;   

my $exp;
my $query =$dbh->selectall_arrayref("SELECT * FROM UsersList " ." where ".
     $filterexpression);
my $numOfRec=@$query ;
my $numofPages = ceil($numOfRec / $offset);
my $sql = "SELECT UserId,UserName,CardNo,GroupId,Role,VerifyType FROM UsersList " ." 
    where ". $filterexpression;
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");
 my $query=$dbh->selectall_arrayref('SELECT
     UserId,UserName,CardNo,GroupId,Role,VerifyType FROM UsersList where '. 
      $filterexpression);
 my $exp = @$query;   
 while (my ($UserId,$UserName,$CardNo,$GroupId,$Role,$VerifyType) = $sth-
    >fetchrow_array())
   {
    push(@overduedata,{UserId=>$UserId,UserName=>$UserName,
   CardNo=>$CardNo,GroupId=>$GroupId,Role=>$Role,VerifyType=>$VerifyType});
    }
   my $startDisplay = ($pageNum)*$offset;  
   my $endDisplay = ($pageNum + 1 ) * $offset;  
   $i = $startDisplay; 
   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 class=\"tablestyle\">";
   print "<tr border=1 style='background-color:#CDC9C9'>
   <td class=\"tdstyle\" colspan=\"2\"></td>
   <td><A HREF=\"http://localhost/cgi-bin/AddUser.cgi\"&gt;ADD&lt;/A&gt;&lt;/td&gt;
   <td><b>UserId</b></td>
   <td><input type=\"text\" name=\"User_Id\"  size=\"6\"></td>
   <td><b>UserName</b></td>
   <td><input type=\"text\" name=\"User_Name\"  size=\"10\"></td>
   <td><input type=\"submit\" name=\"Filter\" value=\"Filter\" ></td>
   </tr>";

  print "<tr class=\"trstyle1\"></tr>";
  print "<tr class=\"trstyle2\">";
  print "<th  class=\"thstyle\"><A HREF=\"http://localhost/cgi-bin/Filtering.cgi?
     Sortype=1\" class=\"hiddenlink\">$sth->{NAME}->[0]</A></th>";
  print "<th colspan=\"2\" class=\"thstyle\">A HREF=\"http://localhost/cgi-
     bin/Filtering.cgi?Sortype=2\" class=\"hiddenlink\">$sth->{NAME}->[1]</A></th>";
  print "<th  class=\"thstyle\"><A HREF=\"http://localhost/cgi-bin/Filtering.cgi?
     Sortype=3\" class=\"hiddenlink\">$sth->{NAME}->[2]</A></th>";
  print "<th  class=\"thstyle\"><A HREF=\"http://localhost/cgi-bin/Filtering.cgi?
     Sortype=4\" class=\"hiddenlink\">$sth->{NAME}->[3]</A></th>";
  print "<th  class=\"thstyle\"><A HREF=\"http://localhost/cgi-bin/Filtering.cgi?
     Sortype=5\" class=\"hiddenlink\">$sth->{NAME}->[4]</A></th>";
  print "<th  class=\"thstyle\"><A HREF=\"http://localhost/cgi-bin/Filtering.cgi?
     Sortype=6\" class=\"hiddenlink\">$sth->{NAME}->[5]</A></th>";
  print "<th  class=\"thstyle\">&nbsp;EDIT</th>";
  print "<th  class=\"thstyle\">&nbsp;DELETE</th>";
  print "</tr>";

   if($exp != 0)
     {
     while ($i < $endDisplay)
       {
      if($i == $exp){
       last;}
   print "<tr class=\"trstyle3\">
   <td >" . $overduedata[$i]->{'UserId'} .  "</td>
   <td colspan=\"2\" >" . $overduedata[$i]->{'UserName'} . "</td>
   <td>" . $overduedata[$i]->{'CardNo'} . "</td>
   <td>" . $overduedata[$i]->{'GroupId'} . "</td>
   <td>" . $overduedata[$i]->{'Role'} . "</td>
   <td>" . $overduedata[$i]->{'VerifyType'} . "</td>
   <td><A HREF=\"\">EDIT</A></td>
   <td><A HREF=\"\">DELETE</A></td>
   </tr>";
  $i = $i + 1;
     }
      }

  if ( $pageNum > 0 ) {
      print q[<td><a href="Filtering.cgi?pageNum=0&Sortype=$val">|&lt;</a></td>];  
         $pageN  = $pageNum - 1;
     print qq[<td><a href="Filtering.cgi?pageNum=$pageN&Sortype=$val">&lt;</a></td>];
     }
    else
    {
      print q[<td><span class="currentpage">|&lt;</span></td>];
      print q[<td><span class="currentpage">&lt;</span></td>];
     }
    if ( $pageNum < ( $numofPages - 1 ))
    {
   $pageN  = $pageNum + 1;
   print qq[<td><a href="Filtering.cgi?pageNum=$pageN&Sortype=$val">&gt;</a></td>];
   $numofPages=$numofPages-1;
   print qq[<td><a href="Filtering.cgi?
      pageNum=$numofPages&Sortype=$val">&gt;|</a></td>];
     }
   else {
          print q[<td><span class="currentpage">&gt;</span></td>];
          print q[<td><span class="currentpage">&gt;|</span></td>];
       }

   print "</table>";
+3  A: 

Client side table sorting is achieved with Javascript. There are dozens of libraries easily found by a simple Web search. Stuart Langridge's sorttable is very easy to implement.

daxim
Thank You, but its not working.I downloaded the JScript and placed it in cgi-bin and in the <head> tag gave this path. <script src="C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\sorttable.jssorttable.js"></script> then <table class="sortable"> but it displayed only the table data thas al.Is that because am placing print "Content-type: text/html\n\n"; before the html code? Please guide me.
sonya
There are two reasons why this will not work. ⑴ You put the Javascript file into the directory reserved for server-side (CGI) scripts. The Apache webserver could try to execute the script as CGI program instead of just transmitting the file contents to the browser who requested that resource. The Javascript file is not a CGI program, this this will not work. ⑵ `C:\Pro…` is a local path. You cannot use that on Webpages, modern browsers will refuse to load this script because of security (local files are prohibited in a Web context), and besides it will not work [continued…]
daxim
for anyone else but you. (Mind what happens when a different user's browser sees that `C:\Pro…` path - it refers to a location at *his* computer!) – The necessary change is to move the JS file into a different directory, and reference it with a Web server path, e.g. if you made a subdirectory named `static` in your `htdocs` Web server root directory, then you can reference it in the `src` attribute with the path `/static/sorttable.js`.
daxim
@sonya Your "its my humble request" is not that small. There are many serious problems with this script before you get to the issue of sorting by table headers.
Sinan Ünür
+2  A: 

This is not an answer to your question, but general advice. Therefore, I have made it community wiki.

Please stop writing CGI scripts for a while until you understand why your script has serious problems.

You have:

use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
# ...
my $query = new CGI;
my $q = new CGI;

First, note that you need to initialize a CGI object only once. Avoid indirect method calls:

my $cgi = CGI->new;

I know the CGI.pm docs use $query but I find $cgi to be more meaningful.

That is a good step. Almost all CGI scripts should use well established libraries rather than homebrew code. However, after that good first step, you do:

print "Content-Type: text/html\n\n";
$query = $ENV{'QUERY_STRING'}; 
@list = split( /\&/, $query);  
 foreach (@list) {
  ($var, $val) = split(/=/);
  $val =~ s/\'//g;
  $val =~ s/\+/ /g;  
  $val =~ s/%(\w\w)/sprintf("%c", hex($1))/ge; 
  ($var, ' = ', $val,); 
 }  

There is no reason to engage in cargo-cult practices. Your CGI object already has the parameters passed to the script.

Also, you should declare your variables where they are first used as opposed to dumping all of them at the script.

Use CGI.pm's header to send the header. You have:

 print <<END_HTML;
 <html>
 <head><title></title>
 </head>
 <body>
 <form action="Filtering.cgi" method="post">
 <TABLE>
  <TR>
   <TD>
     <input type="hidden" name="submit" value="Submit">
   </TD>
 </TR>
 </TABLE
 </form>
 </body></html>
 END_HTML

which makes no sense as you have sent a complete HTML document before doing anything else in the script.

The rest of the code cannot change what you have already sent.

Put your HTML in a template. Personally, I like HTML::Template for the clean separation between code and content.

This way, you can write your Perl script to generate the content and include any client side functionality in the template separately.

Sinan Ünür
This is a very useful information, million thanks for this knowledge. i will follow them all.
sonya
Yes, always debug and explore in small steps.
brian d foy