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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Item from Database



Jan 22, 2001, 9:40 AM

Post #1 of 3 (334 views)
Item from Database Can't Post

Hi All:

Stopped by about a week ago and since then have been scouring the posts in all forums. Lot's of great info...

I have a program I'm working on that deals with a delimeted db.


310|Accessories|27.99|Projen Deluxe Travel Cover|L%%aProAcc.jpg|Projen Deluxe Travel Cover is New for 2000|[no options available]|8
311|Accessories|14.95|Callaway 'Cool Weather' Hat|I%allawayhat.jpg|For the golfer who doesn't know when to stay of the course in cool weather. Great Gift|O%08|8
2003|Woods|74.95|Mizuno Titanium Driver|I%%wmp19tzd.jpg|Mizuno T- Zoid T3 Titanium Driver.|O%05%06|8
2004|Woods|79.95|Mizuno Widec Woods|I%%wMizWidecWd.jpg|Mizuno Widec Wood.|O%05%06|8
2015|Irons|349.95|Maxfli Revolution Irons|L%%mp28tzti.jpg|Maxfli Revolution Irons.|O%05%06|8
2016|Irons|189.95|Meridian Missle Irons|L%%MerMisIr.jpg|Meridian Missile Iron. Precision made Irons|O%05%06|8

There can be numerous products under each category. What I would like to do is take each category, even tho duplicates exist and bring each out just once, so that I can make a link to that category.

In the example, it would be Accessories, Irons and Woods. I would then like to take those three and print them to a file within the link that the script can read.

<a href=\"\%\%sc_link\%\%\&product=$category\">$category<\/a>\n";

This file is then read by another portion of the script when a customer updates his catalog page and the links appear.

I've got that part working fine, I just need to be able to read the categories once from the database.

Any help would be appreciated...


User / Moderator

Jan 22, 2001, 1:15 PM

Post #2 of 3 (326 views)
Re: Item from Database [In reply to] Can't Post

Is this what you want?

my @data = ("310|Accessories|27.99|Projen Deluxe Travel Cover|LaProAcc.jpg|Projen Deluxe Travel Cover is New for 2000|[no options available]|8", 
"311|Accessories|14.95|Callaway 'Cool Weather' Hat|Iallawayhat.jpg|For the golfer who doesn't know when to stay of the course in cool weather. Great Gift|O08|8",
"2003|Woods|74.95|Mizuno Titanium Driver|Iwmp19tzd.jpg|Mizuno T- Zoid T3 Titanium Driver.|O0506|8",
"2004|Woods|79.95|Mizuno Widec Woods|IwMizWidecWd.jpg|Mizuno Widec Wood.|O0506|8",
"2015|Irons|349.95|Maxfli Revolution Irons|Lmp28tzti.jpg|Maxfli Revolution Irons.|O0506|8",
"2016|Irons|189.95|Meridian Missle Irons|LMerMisIr.jpg|Meridian Missile Iron. Precision made Irons|O0506|8");

my %hash;

foreach (@data) {
my $category = (split ('\|', $_))[1];
$hash{$category} = 1 unless (exists ($hash{$category}));

my @links = map { '<a href="%%sc_link%%&product=' . "$_\">$_</a>\n" } sort keys %hash;
print @links;

Which will print these three lines

<a href="%%sc_link%%&product=Accessories">Accessories</a> 
<a href="%%sc_link%%&product=Irons">Irons</a>
<a href="%%sc_link%%&product=Woods">Woods</a>

For each row in the data it does a split and only looks in the 1 position where the category lives. Then it adds it to a hash if it is not there yet.

And finally we build an array of links using the keys from that hash. Note that I split the link into two parts so I could get away with fewer backslashes because you don't have to worry about them inside single quotes.

Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


Jan 26, 2001, 11:42 AM

Post #3 of 3 (306 views)
Re: Item from Database [In reply to] Can't Post


Your solution worked perfectly

I appreciate your time and knowledge




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

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