Gossamer Forum
Home : Products : DBMan : Customization :

Output a text file

Quote Reply
Output a text file
Hello all,

First off, I am virtually non-conversant in Perl/CGI when it comes to programming. I can install Perl programs and cut-n-paste mods but that is the extent of it Wink <flattery>Between JPDeni and Eliot and others I am blown away by your grasp of this interface and especially DBMan.</flattery>

What I'd like to do is this:
We have a used book DBman running for our library. When I update DBman I'd like to be able to select certain fields to output to a text file - one record per line so that our library supporters could then download it as sort of a printable catalog.
ie: I select Author, Title, Publisher and then hit submit and DBman generates an output.txt in the aforementioned format. Is this possible? Thanks in advance for any insights you may be able to offer.
Quote Reply
Re: Output a text file In reply to
Thanks for the compliments...But Carol is the real whiz around these parts! She knows way more about Perl and DBMAN than I do. She deserves most,if not, all the credit.

Smile

That is a wonderful idea. I was thinking about doing something like that with a DBMAN project I have in the works, which is a National Anthropology Membership Directory...I was thinking of adding a feature that would allow people to download a set of records into a database file that could be imported into their email or other electronic address books.

I hope that Carol or others will be able to direct us in the right direction.

(One idea that just came to me that you might think of doing is creating another "html" output sub-routine and use <PRE> anchors without any other HTML format to create a text file like output.)

The checkboxes and selecting specific fields to be printed would have to be advised by Carol or others.

Great idea!

Regards,



------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
Quote Reply
Re: Output a text file In reply to
Hi Eliot,

<sidebar>
I noticed this in your signature:

Coconino Community College
http://www.coco.cc.az.us/

My Mom lives in Cottonwood AZ right on the edge of the Coconino National park/forest.
</sidebar>

Thanks for the quick reply Eliot - it is something I've thought about for awhile as well but due to my lack of Perl skills I'm afraid I'm just a dreamer at this point.



------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
<sidebar>
Cottonwood is only about 40 minutes from Flagstaff. Nice area.
</sidebar>

Another thought I had was may be creating another html search file that would have the fields that you want to "print" in the text file. What you could do is create a sub-routine similar to the sub-routine where you have your search form, but only provide the fields that you want to be printed into the "text" file. Then you either allow the library supporters to choose a number of records or you set the form to print all the records to print on each page. I think the former is a better option because some people may not want to wait for a whole set of records to download on one page.

Then you could create another sub-routine similar to the html_record sub-routine, but format it so that only the fields specified in the second search form will appear.

This is off the top of my head. If this is route that you want to go, then I can assist you with some codes that may get you started. But if you are looking for a seamless approach where there is one search form and then people click on the fields that they want printed, then JPDeni (Carol) may be able to help you.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited August 29, 1999).]
Quote Reply
Re: Output a text file In reply to
First off, thanks much for the compliments, both of you. Smile

Hooter, you can certainly save a text file of whatever you want and it can be in any format you want. It can be the results of any search you want. You can format results to be a printable page from the browser or to save a text file.

Whatcha need?


------------------
JPD





Quote Reply
Re: Output a text file In reply to
 
Quote:
First off, thanks much for the compliments, both of you.

You're very welcome JPDeni...You've got to be the most tireless and helpful person I've ever seen in a support forum. Eliot ain't so bad either Wink

Quote:
You can format results to be a printable page from the browser or to save a text file. Whatcha need?

Saving to a text file for download would be preferable at this point along with a sort by selector before output? I don't ask for much do I JPDeni? Smile
Is this possible?



------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
I think so. What I need is for you to tell me in detail about what you want to happen. Who does the search? What fields (including field names) would be included? What format do you want the file in? How would users be reading it? Could you give me a sample of how you want a record to look?

Quote:
Saving to a text file for download would be preferable at this point along with a sort
by selector before output?

You mean sorted before the file is saved? That's fine, because the script would just go through a regular search routine to get the records and that includes a sort.



------------------
JPD





Quote Reply
Re: Output a text file In reply to
 
Quote:
I think so. What I need is for you to tell me in detail about what you want to happen. Who does the search? What fields (including field names) would be included? What format do you want the file in? How
would users be reading it? Could you give me a sample of how you want a record to look?

I'd be the only one doing the search to create this downloadable text file for the users. Format would ASCII text-if I'm understanding your question correctly? Fixed length fields based on my dbman.cfg so that the output fields line up correctly and a wrap if record extended beyond say column 70 then a line break between each record ie:

Title Author Publisher
rec01title rec01author rec01publisher
<BR>
rec02
<BR>
rec03
<BR>
...and so on
Does that make any sense JPDeni?



------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
 
Quote:
I think so. What I need is for you to tell me in detail about what you want to happen. Who does the search? What fields (including field names) would be included? What format do you want the file in? How would users be reading it? Could you give me a sample of how you want a record to look?

Hi JPDeni...Did you forget about me? I understand as I know you're really busy. Without being insensitive or uncouth I've heard that you're fond of Amazon gift certificate which I can gladly provide as this MOD would be a valued addition for our library supporters.



------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
I didn't forget about you. I just missed your post. It happens sometimes, especially when people are posting at the same time as I am. I lose track of which posts I've responded to and which ones I haven't. I'm sorry.

But you did the right thing in adding another response to the thread to bring it up to the top. Smile

You said
Code:
|;
if ($per_admin && $in{'view_search'}) {
print qq|
<tr><td><input type="checkbox" name="printout" value="1"> Send results to text file</td></tr>
|;
}
print qq|

When you do your search, be sure to set the "Maximum Hits" number high enough to account for all the records that might be returned.

In sub html_view_success, after

my (@hits) = @_;

add

Code:
if ($in{'printout'}) {
&html_printout(@hits);
return;
}

Now we get to create the new subroutine -- sub html_printout. I think it might be best, for the time being, to have the text printed to the browser. Once you know your formatting is like you want it, you can save the file.

I started to write the subroutine, but I'm still not quite sure about what you want to do with the length of the fields. You can go ahead and put the above code in, but if you try to use it, you'll get an error message that the subroutine is not defined. We'll get it written, though. (And I'll make a special effort to watch for your response. Smile )

If you would like to give me a gift certificate, that would be very nice, but it's definitely not required. It was merely an oversight on my part. Smile


------------------
JPD





Quote Reply
Re: Output a text file In reply to
 
Quote:
I started to write the subroutine, but I'm still not quite sure about what you want to do with the length of the fields. You can go ahead and put the above code in, but if
you try to use it, you'll get an error message that the subroutine is not defined.
We'll get it written, though. (And I'll make a special effort to watch for your response.)

Hi JPDeni,
I guess what I meant about the fixed-length fields was that if I selected fields that ended up being over say a 70 character limit, then each record display would wrap - Now that I think of it - if its easier just have one field per line display then a break between each record ie:

Title1
Author1
Publisher1

Title2
Author2
Publisher2

and so on. I know that any one of my single fields is not more than 70 characters.

------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
Oh, this is much easier!! Smile

Create a new subroutine in html.pl.

Code:
sub html_printout {

my (@hits) = @_;
my ($numhits) = ($#hits+1) / ($#db_cols+1);

$output = qq|Insert anything you want as introductory text here.
You can just put carriage returns within the text for new lines.
Be sure to end with \n\n in order to get a double space
Before the start of the record printout.
|;

for (0 .. $numhits - 1) {
%rec = &array_to_hash($_, @hits);
$output .= "$rec{'Title'}\n$rec{'Author'}\n$rec{'Publisher'}\n\n";
}

open (TEXT, ">/full/path/to/text.txt") or
&cgierr("error in writing text file. \nReason: $!");
if ($db_use_flock) {
flock(TEXT, 2) or &cgierr("unable to get exclusive lock on text file.\nReason: $!");
}
print TEXT $in{$db_key};
close TEXT;

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: Text File Saved</title>
</head>
<body bgcolor="#DDDDDD">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: Text File Saved</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>Text File Saved
</b></font></center><P>
<$font>
The text file was successfully saved.</FONT>
<P>|;
&html_footer;
print qq|</td></tr>
</table></center></body></html>|;
}

------------------
JPD







[This message has been edited by JPDeni (edited September 02, 1999).]
Quote Reply
Re: Output a text file In reply to
Hi JPDeni,

I pasted all the code as you directed, the process runs without error (I get the screen with 'print file has been successfully saved') but my text file output is always 0 bytes. If I put an * in any of my fields to get all records then text file equals 1 and just has the * inside. Any ideas? - Did I do something wrong? Thank you very much for your efforts.


------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
I don't know. It seems like it would work.

Try changing every reference to hits to hits2. There might be some conflict with variable names.

@hits becomes @hits2
$#hits+1 becomes $#hits2+1


You are getting search results when you don't check the "printout" checkbox, right?


------------------
JPD





Quote Reply
Re: Output a text file In reply to
Hi JPDeni,

Quote:
Try changing every reference to hits to hits2. There might be some conflict with
variable names.

@hits becomes @hits2
$#hits+1 becomes $#hits2+1

Did this - same result.

Quote:
You are getting search results when you don't check the "printout" checkbox, right?

You betcha, just like I always would. But if I check 'send results to text file', process completes but nothing is output to text file. I don't even get the preliminary text that you demonstrated/defined with $output



------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
You have the path correct, or else you wouldn't have gotten a "*" in the file before. And the directory must be writable, because you aren't getting an error message and a file does exist.

Let me look over the code and see if I did something really stupid.

I did something really stupid.

I was copying code from the script and didn't change it all.

change

Code:
print TEXT $in{$db_key};

to

Code:
print TEXT $output;

Boy, was that dumb!! (Maybe my cats were distracting me at that moment. Yeah, that's it! Blame it on the cats!! Smile )


------------------
JPD





Quote Reply
Re: Output a text file In reply to
 
Quote:
print TEXT $in{$db_key};
to
print TEXT $output;

Hi JPDeni,
Did the above and now at least we get some output to the text file but alas it's only the $output string of text - no records are being output. You're a marvel to put up with us novices like this. Wink


------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
Well, at least you're getting something. Smile

Are you getting a bunch of blank lines in the file, or does it just stop after the introductory text?

Are your fields named Title, Author and Publisher? They must be exactly that, and not title or TITLE or anything else. If they aren't exactly the same as I have them in the script, change the script to match your field names.

If that doesn't seem to be it, just after

Code:
The text file was successfully saved.</FONT>
<P>|;

add

Code:
foreach $column (@db_cols) {
print "$column: $rec{$column}<BR>";
}

If things are working, you should get the information for the last record that was returned from the search.

(Just trying to narrow down where the problem is.)



------------------
JPD





Quote Reply
Re: Output a text file In reply to
Hi JP,

I don't know if in between one of the code changes I forgot to ftp the revised code but it now works beautifully! You are without a doubt a genius when it comes to this! I guess the only problem now is that my publisher field does run on past 70 characters (it includes little publishing details that I forgot about) and I need a way to wrap it. If and when you have a chance JP, could you look at a way to wrap the text if >70 characters.
Thank you very much.


------------------
Regards,
jheath1@home.com
Quote Reply
Re: Output a text file In reply to
Yay!!! It works!!!! Yay!!!!!!! Smile



------------------
JPD





Quote Reply
Re: Output a text file In reply to
To send me a gift certificate from Amazon (how nice of you! Smile ), just go to amazon.com and follow the directions for buying a gift certificate. They will ask for my email address. Use hall@drizzle.com. Thank you!! Smile

To get the date, after

my ($numhits) = ($#hits+1) / ($#db_cols+1);

add

$date = &get_date();

Then, within the next section, use the variable $date to print the date.

So you would have

Code:
$output = qq|My text blah blah blah $date edition|;






------------------
JPD







[This message has been edited by JPDeni (edited September 03, 1999).]
Quote Reply
Re: Output a text file In reply to
JP,

You are a wizard!!! I substituted the word-wrap code and it works perfectly! I would sincerely like to send you a gift certificate for Amazon - or anyplace of your choosing for that matter. How would you like me to handle it - is there a way I can post it to your account on Amazon? - I don't know as I never shop there.
Addendum:
JP you're probably gonna regret you ever responded to my thread but here goes:
How do I insert today's date (the date at which I create the file) into my text header?
I tried this:
right after your sub html_printout {
Code:
&get_date
Then in my header text after the $output = qq|
Code:
print "$day-$months[$mon]-$year";
What happens is I get a navigator window popping up asking me if I want to download an httpd:cgi application. What I'd like to end up with is :

My text blah blah blah $DATE edition

The header text is output fine as you wrote it - it just screws up when I try to get the date in there.

------------------
Regards,
jheath1@home.com

[This message has been edited by hooter (edited September 03, 1999).]
Quote Reply
Re: Output a text file In reply to
  
Quote:
To send me a gift certificate from Amazon (how nice of you! ), just go to
amazon.com and follow the directions for buying a gift certificate. They will ask for
my email address. Use hall@drizzle.com. Thank you!!

You are most welcome JP. Everything you coded/instructed me to do works perfectly! Thanks again - Payday is on the 10th so I hope you can hold out till then Wink

Addendum/Final Question
Hi JP,

In my outputted text file how do I get the $date to format as Mon-dd-yyyy as opposed to the current format which is dd-Mon-yyyy
------------------
Regards,
jheath1@home.com

[This message has been edited by hooter (edited September 04, 1999).]
Quote Reply
Re: Output a text file In reply to
I just wanted to say thanks, this thread helped me out BIG time!

| www.meltedmonitor.com |