Gossamer Forum
Home : Products : DBMan : Customization :

If statement. Please help!!

Quote Reply
If statement. Please help!!
In my sub html_add_success ,

I use if statement so it can send mail to us:
Code:
my (%rec) = &get_record($in{$db_key});

if ($rec{'age'} >= '60') {

open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To:$oldage\n";
print MAIL "From: $admin_email\n";
print MAIL "For Old men are here>\n";
print MAIL "-" x 75 . "\n\n";

foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}

elsif ($rec{'state'} eq 'California') {

open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To:$state\n";
print MAIL "From: $admin_email>\n";
print MAIL "Californian only>\n";
print MAIL "-" x 75 . "\n\n";

foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}

elsif ($rec{'Year'} == '2000') {

open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To:$y2k\n";
print MAIL "From: $admin_email\n";
print MAIL "Year of 2000>\n";
print MAIL "-" x 75 . "\n\n";

foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}

open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To: $email\n";
print MAIL "From: $admin_email\n";
print MAIL "Subject: $rec{'ID'} is in our Database\n\n";
print MAIL "-" x 75 . "\n\n";
print MAIL "The following new record has been added to the database:\n\n";
foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}

1. every record will be mail to email
2. If age >=60 then mail to old man
3. If state is california then mail to californian
4. If year = 2000 then let us know it belong to Y2K

Why I got mail step 1 AND/ OR Step 2 but never have mail if state is california , I never have mail if year= 2000.

Could you help me to find out.

Thanks,
Quote Reply
Re: [britneythuyduong] If statement. Please help!! In reply to
perl uses different operators for math and string comparisons as you have done in your code.

but,

Code:
($rec{'Year'} == '2000' should either be

($rec{'Year'} eq '2000' or

($rec{'Year'} == 2000


depending on how you defined Year in your default.cfg


Age appears to be the same way


Is it california, californian, California or Californian?

If the IF ain't working, then the operator is the wrong type or the values being compared are not the same.


That should get you started...the experts will have to handle anything more.

One last thing. Since you are using the ELSIF, your IF statement may not logical.

For example, if the age is greater than 59, then the state and year comparisons are NEVER done. If the sate is less than 60 and the state is california then the year comparison is NEVER done. Maybe that is what you want. I can't tell for sure. You may need to rethink the conditional expression since you are changing the the terms - using age in one, state in another and year in yet another.


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] If statement. Please help!! In reply to
Hi Gene,

Yes, I will use year == 2000,
at first I use
if age >=60 then
if state then
if year == 2000 then
but it still not working. Only if age >=60 and each record will mail. It never mail when state eq California or year == 2000.

Can we have alot if in here ? why one the first one can use it ???

Regards,Unsure
Quote Reply
Re: [britneythuyduong] If statement. Please help!! In reply to
I really hope some of the Perl experts come along for this one, but, let's see if I can help a little. (a little more that is - good work Gene)


Quote:
if ($rec{'age'} >= '60')
Since the 60 is in quotes I will hazard a guess that it is being evaluated as a string and not as a number.
Which I am guessing it is evaluating as zero, thus all records meet this criteria and no further execution of the elsif are made.

I would try commenting out all the other "conditions" and make sure each if works by itself.

The way you have it with the elsif, if the arguements worked, old men from california would be sent to old men and not to california. Year = 2000 would only be sent if not old men and not california.

Not sure what the desired results is, but if you can explain it, we may be able to help you design the logical if statement(s).

joe
Quote Reply
Re: [joematt] If statement. Please help!! In reply to
Ok a few points:

Quote:
($rec{'Year'} == '2000' should either be ($rec{'Year'} eq '2000' or ($rec{'Year'} == 2000

...thats not totally true. eq is for strings and == is for numerics so the correct way is:

$rec{Year} == 2000

Quote:
Since the 60 is in quotes I will hazard a guess that it is being evaluated as a string and not as a number.
Which I am guessing it is evaluating as zero, thus all records meet this criteria and no further execution of the elsif are made.

Quoting it will work the same way as not quoting. You will still get the correct outcome.

Last edited by:

Paul: Feb 24, 2003, 2:51 PM
Quote Reply
Re: [Paul] If statement. Please help!! In reply to
Thanks for the info Blush...just out of curiosity what would it be if Year is defined as alpha instead of numeric?


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [Paul] If statement. Please help!! In reply to
Hi paul,

I did use if statement first but it did not work, so I changed to elsif and it did not work too Blush,

Book did not teach multiple if Blush, it only work with the first if, the second and third if will not count??

Why ?? Please help me to understand this.

Thanks,
Quote Reply
Re: [britneythuyduong] If statement. Please help!! In reply to
show us your db_def section in your links.def file...or at least the ones for age, state and year

for example

State => [36, 'alpha', 30, 30, 0, '', ''],
Year => [44, 'numer', 4, 4, 0, '', ''],
Age => [72, 'numer', 3, 3, 0, '', ''],


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] If statement. Please help!! In reply to
Hi esm,

I found my mistake, I fixed it and it works perfectly.

Thanks,Blush
Quote Reply
Re: [britneythuyduong] If statement. Please help!! In reply to
can you share with us what the solution was?


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] If statement. Please help!! In reply to
Hi Gene,

Here is my example, and we should use if statement only, the problem I had before and it did not work because I did not type correct email address of $state and $y2k, this mistake because I used cut and paste the bad address Blush

Code:



my (%rec) = &get_record($in{$db_key});

if ($rec{'age'} >= '60') {
open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To:$oldage\n";
print MAIL "From: $admin_email\n";
print MAIL "For Old men are here>\n";
print MAIL "-" x 75 . "\n\n";

foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}

if ($rec{'state'} eq "California") {

open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To:$state\n";
print MAIL "From: $admin_email>\n";
print MAIL "Californian only>\n";
print MAIL "-" x 75 . "\n\n";

foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}

if ($rec{'Year'} == 2000) {

open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To:$y2k\n";
print MAIL "From: $admin_email\n";
print MAIL "Year of 2000>\n";
print MAIL "-" x 75 . "\n\n";

foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}

open (MAIL, "$mailprog") || &cgierr("Can't start mail program");
print MAIL "To: $email\n";
print MAIL "From: $admin_email\n";
print MAIL "Subject: $rec{'ID'} is in our Database\n\n";
print MAIL "-" x 75 . "\n\n";
print MAIL "The following new record has been added to the database:\n\n";
foreach $column(@db_cols){
print MAIL "$column: $in{$column}\n";
}
close (MAIL);
}
TDBlush
Quote Reply
Re: [britneythuyduong] If statement. Please help!! In reply to
great...in that case, your are spared the "hundred lashes with a wet noodle." Wink

and if I had $10 for everytime I have done something like that, we BOTH could retire today! Crazy


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."