CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
reading from Txt db to create form dropdown?

 



BrightNail
Novice

Apr 16, 2002, 7:35 PM

Post #1 of 6 (878 views)
reading from Txt db to create form dropdown? Can't Post

hey all,

I have a "ratings script" and I want to add a function in which a certain product can have "information links"..like so..

aloe vera juice: see links
(drop down menu here)
1. link one
2. link two
etc..in html dropdown format

anyways,
basically, I have my script opening a txt database and formatting some things, but I want to open another one txt database that has these link listings..I'll probably format it like so....but remember, I have x number of products..so, each line in the "new" database corresponds to the products database...lets say if it says 'yes' it then formats the FORM dropdown, if it says 'no' it doesn't...ala

yes | linkonename: link | linktwoname:link |
no|
yes| another line of links....

so basically, lines 1 and 3 above would correspond to products (in my other database) 1 and 3 and they would have a dropdown html menu....

(select name=whatever)
(option value="link on link")link one description
(option value="link two link")link two description

basically, I need the code to read the txt format above and create a dropdown..

any help? man, that was a lonnnnnggg explanation...


ka0osk
Novice

Apr 18, 2002, 7:59 AM

Post #2 of 6 (863 views)
Re: [BrightNail] reading from Txt db to create form dropdown? [In reply to] Can't Post

Do you mean you want to do this? (pardon my typos)

$cat_path = "yourpathgoes here";
open(DATAFILE, $cat_path) || die;
while ($line = <DATAFILE>)
{
push @product_cat, $line;
}
close (DATAFILE);
print qq~
<SELECT NAME="product">
<OPTION> ~;
foreach $prodcat (@prodcat)
{
print "<OPTION>$prodcat";
}
print qq~ </SELECT> ~;



Unimpressed___________
John Step;


yapp
User

Apr 18, 2002, 9:44 AM

Post #3 of 6 (859 views)
Re: [ka0osk] reading from Txt db to create form dropdown? [In reply to] Can't Post

Nice Smile

Some minor suggestions:


Code
use Fcntl qw(:flock); 

my $cat_path = "yourpathgoes here";
open(DATAFILE, $cat_path) or die "Can't open $cat_path: $!"; # Nice die message
flock(DATAFILE, LOCK_SH); # Locked!
my @product_cat = <DATAFILE>; # Faster!
close (DATAFILE);

chomp @product_cat; # remove all trailing \n 's

print qq[<SELECT NAME="product">\n];
my $I = 0;
foreach my $prodcat (@prodcat)
{
print qq[<OPTION value="$I">$prodcat</OPTION>\n];
$I++; # You might want to use a different ID (or remove the value="...")
}
print "</SELECT>\n";


Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

(This post was edited by yapp on Apr 18, 2002, 9:44 AM)


ka0osk
Novice

Apr 18, 2002, 12:09 PM

Post #4 of 6 (854 views)
Re: [ka0osk] reading from Txt db to create form dropdown? [In reply to] Can't Post

re: my @product_cat = <DATAFILE>; # Faster!

Cool! I will try that on my own code.

and I forgot to put that value= into option...DOH!

TNX!

John Step - Typo Kyng


BrightNail
Novice

Apr 18, 2002, 9:24 PM

Post #5 of 6 (850 views)
Re: [yapp] reading from Txt db to create form dropdown? [In reply to] Can't Post

hey thanks,,

I have some questions if you don't mind regarding your code...

1. why do you preceed @product_cat with a "my". I've seen it before but never explained fully?

2. explain this line - my $I = 0;
what does the $I stand for? Is it a counter? it seems like that to me..


yapp
User

Apr 19, 2002, 12:08 AM

Post #6 of 6 (849 views)
Re: [BrightNail] reading from Txt db to create form dropdown? [In reply to] Can't Post

$I is a counter Yes! Smile I used $I in the <OPTION value="$I"> line. The $I++ line increments the counter.

About the [url=http://www.perldoc.com/perl5.6.1/pod/func/my.html]my statement:
You can use my() to declare a variable in a BLOCK. Using my() scopes the variable, so you can only use that in that BLOCK.

When you're not using my, every variable you use will be allocated in the package symbol table (%main:: for example) Perl uses the symbol table to keep track of all global variables, so they can be accessed through the entire program. In other words, memory is never freed or re-used, since the variable exists as global in the symbol table! Therefore, most Perl Programmers consider using my() a better coding style.

If you want to make a global variable anyway, you can use the [url=http://www.perldoc.com/perl5.6.1/lib/vars.html]vars pragma, or the [url=http://www.perldoc.com/perl5.6.1/pod/func/our.html]our statement as of perl v5.6.0.

The [url=http://www.perldoc.com/perl5.6.1/lib/strict.html]strict pragma forces declaration of all variables through my() in your .pl file. In that case, making a typoo in a variable name results in an error (and not a new global variable)

example:

Code
#!/usr/bin/perl -w 

use strict; # forces declarations through my()
$x = 'fdfdsf'; # ERRORS: global symbol $x requires explicit package name

my $x;
$x = 'fsdfdsfds'; # Now you can use $x

my $y = $x; # declare and assign a value

sub dosomething
{
my($param1, $param2) = @_; # I love this Perl feature!!

print "PARAM1 = $param1, PARAM2 = $param2\n";
}

dosomething($x, $y);

$param1 = 'fdfds'; # ERRORS! $param1 is not declared in this block, but in "sub dosomething()"


Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives