Creating CSV file from TABLE HTML

I want to write a PERL CGI that will:
(1) Receive data from an SQL SELECT Result Set and create an HTML Table filled with the data returned.
(2)  When a button labeled 'DOWNLOAD' is selected (will be created above the table in the HTML)I want to create
a CSV file on the users PC.

Can you help?
akellerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
JFMConnect With a Mentor Commented:
pasting the answer again from my last comment.

install the  Win32::ODBC module

     this code creates a html table from query results:

     use Win32::ODBC;

     $DSN = "MyDSN";
     $UID = "akeller";
     $PWD = "";

     $db = new Win32::ODBC("DSN=$DSN;UID=$UID;PWD=$PWD;") || die "couldn't
     open connection";

     $stmt = "SELECT * FROM Foo";
     $rc = $db->Sql($stmt);
     die qq(SQL failed "$stmt": ), $db->Error(), qq(\n) if $rc;

     $table = "<TABLE>\n";
     while ($db->FetchRow()) {
     $table .= "<TR>";
     # get all columns, you can fetch them by name, either: $db->Data("columnName");
     @values = $db->Data;
     foreach (@values) {
     $table .= "<TD>$_</TD>";
     }
     $table .= "</TR>\n";
     }
     $table .="</TABLE>\n";

     $db->Close();

     print $table;
0
 
JFMCommented:
just pasting the answer from the question 'Database' (a little bit modified):

assuming your platform is win32:
You need to do two things:

   1.  Install ODBC driver (for the database you're using) if you haven't already:
           should be found at http://www.microsoft.com
   2.  Download and install the Win32::ODBC module from the following website:
           http://www.roth.net/odbc/

Once you have done this, create an ODBC connection for your database and interface with it using the perl module.  Installation and usage instructions are available on the page mentioned above (along with a test/demo database in the download package.)

2nd question:
I can't see any solution for that problem. A CGI doesn't have any rigths to work on client's side.
Though, it would be possible to open a 'Save As' Window  and download the file to the desired place in the file system.
0
 
akellerAuthor Commented:
I want to read through the SQL Result set buffer creating a file which I would save to the server to allow download of the file to the pc by the customer.  

The question is was (my odbc is already setup) more explicityly, 'What would the loop code in the cgi script look like if I was looping through my SQL result set with to produce a file'.  The customer doesn't just want the data on their screen, they also want a file (CSV) for downloading, so I was looking for an example of that kind of a perl script solution.

Question points uped, please help ... I am new to cgi development.

0
 
JFMCommented:
still not sure, if I understood the question, but i try:
Have you already installed the  Win32::ODBC module?

this code creates a html table from query results:

use Win32::ODBC;

$DSN = "MyDSN";
$UID = "akeller";
$PWD = "";

$db = new Win32::ODBC("DSN=$DSN;UID=$UID;PWD=$PWD;") || die "couldn't open connection";

$stmt = "SELECT * FROM Foo";
$rc = $db->Sql($stmt);
die qq(SQL failed "$stmt": ), $db->Error(), qq(\n) if $rc;

$table = "<TABLE>\n";
while ($db->FetchRow()) {
      $table .= "<TR>";
      # get all columns, you can fetch them by name, either: $db->Data("columnName");
      @values = $db->Data;
      foreach (@values) {
            $table .= "<TD>$_</TD>";
      }
      $table .= "</TR>\n";
}
$table .="</TABLE>\n";

$db->Close();

print $table;

 



0
 
akellerAuthor Commented:
Good Answer from JFM.  Thanks.  Please have JFM lock this question.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.