Gossamer Forum
Home : Products : DBMan : Installation :

Conditional Statements in sub html_record

Quote Reply
Conditional Statements in sub html_record
I have an application where I need to have a different URL action specified depending on a condition in a database field.

In my sub html_record routine, I want to check on the conditon of a certain field, say $rec{'Country'} and then based on the value of Country genertate on URL action verus another. In psuedo code here' what I want:

If $rec{'Country'} = Canada then
<a href="http://gotocanda.com">
else
<a href="http://gotousa.com">

I guess my real question is where do I test for the condition (in what routine -- can it be done in the sub html_record or can I preload the <a href fragments into a variable earlier, e.g., $canda="<a href://gotocanada">
$usa="<a href://gotousa">

Do I need to use javascript to do this in the sub html_record or ???

Quote Reply
Re: Conditional Statements in sub html_record In reply to
You can do it right in html_record.

You'll have some stuff you're going to print to the html page first --

print qq|<table><tr><td>....

In the place you want to put your conditional statement, enter a |; to end the print qq| statement.

Then use

Code:
if ($rec{'Country'} eq "Canada") {
print qq|<a href="http://gotocanda.com">|;
}
else {
print qq|<a href="http://gotousa.com">|;
}

Then start another print qq| statement to print out the rest of your record.

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





Quote Reply
Re: Conditional Statements in sub html_record In reply to
Thanks, as usual you are right on -- the great eye!
Quote Reply
Re: Conditional Statements in sub html_record In reply to
OK this is working great and I though I was able to understand the Perl syntax a bit but now I'm confused.

Here is a code snipet of what I've done and the problem is with the contents of $Country the way I've set it up here, nothing is retrieved from that variable. Apparently it's not being parsed properly the way I've set it up;

This code is in my html_record sub
--------------------------------->
if ($rec{'Country'} eq "Afghanistan") { $Country = "AFG"; }
elsif ($rec{'Country'} eq "Albania") { $Country = "ALB"; }
elsif ($rec{'Country'} eq "Algeria") { $Country = "DZA";}
elsif ($rec{'Country'} eq "American Samoa") { $Country = "ASM";}

print qq|<TD><a href="http://www.mapblast.com/mblast/map.mb?loc=wo&CMD=GEO&AD3=$rec{'City'}&AD4=$Country"> <IMG alt=ViewMap border=0 height=15 src="../../../viewmap.gif" vspace=2 width=90></A></TD>

---------------------->
Note: I've left out the <table> <TR> and so on. All that is working fine. The problem is in my &AD4=$Country clause above. Nothing is retrieved.

How do I do this?

Thanks.


[This message has been edited by pabloa (edited May 14, 1999).]
Quote Reply
Re: Conditional Statements in sub html_record In reply to
Since you probably have a whole lot more countries than you've listed here, it would probably be better to use an associative array than a long series of "if" statements.

I would add the following to the .cfg file, probably just after the Authorization Options section:

Code:
%Countries = ('Afghanistan' => "AFG",
'Albania' => "ALB",
'Algeria' => "DZA",
'American Samoa' => "ASM"
);

Then, in your link you can use

&AD4=$Countries{$rec{'Country'}}

I'm not sure why your series of "if" statements didn't work, though. If you'd like to debug it, try adding a line

print length($rec{'Country'});

and make sure the number is the same as the number of letters in the country for the record. It's possible that there's an extra character at the beginning or end of the field that doesn't show up -- a space or a linefeed.

Make sure you don't have any spaces before or after the commas in the list of options for your select field.

Code:
Right: 'Afghanistan,Albania,Algeria,American Samoa'
Wrong: 'Afghanistan, Albania, Algeria, American Samoa'

Also, be sure all your options for a given select field are on the same line.

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





Quote Reply
Re: Conditional Statements in sub html_record In reply to
The code you suggest I'm sure will solve the problem -- I get the following error when I use it. Any ideas?

Error Message : fatal error: Illegal division by zero at ./html.pl line 838

The 22 characters were because some person modifying their record had input South Australia into the country field (can't do this easily since it's a select drop down --
Anyway that resolves the 22 character issue.

Thanks for all your help!
Quote Reply
Re: Conditional Statements in sub html_record In reply to
OK, I like your idea of the associative arrays in the cfg file rather than the 200+ if elsif statements in the html.pl file.

Still can't get it to work however. Here are some code snipets. Let's say I'm searching for records with the field Australia.

The first snipet is per your recommendation and the associative array. I've only listed through Australia.

%Countries = ('Afghanistan' => "AFG",
'Albania' => "ALB",
'Algeria' => "DZA",
'American Samoa' => "ASM",
'Andorra' => "AND",
'Angola' => "AGO",
'Anguilla' => "AIA",
'Antigua and Barbuda' => "ATG",
'Argentina' => "ARG",
'Armenia' => "ARM",
'Aruba' => "ABW",
'Australia' => "AUS",

----------------------------->
This next snipet is the select field setup in the cfg file for County. There are several select fields, this is just one, hence the trailing comma at the end.

%db_select_fields = (
Country => 'Afghanistan,Albania,Algeria,American Samoa,Andorra,Angola,Anguilla,Antigua and Barbuda,Argentina,Armenia,Aruba,Australia,Austria,Azerbaijan,Bahamas,Bahrain,Bangladesh,Barbados,Belarus,Belgium,Belize,Benin,Bermuda,Bhutan ,Bolivia,Bosnia and Herzegowina,Botswana,Brazil,British Virgin Islands,Brunei Darussalam,Bulgaria,Burkina Faso,Burundi,Cambodia,Cameroon,Cape Verde,Cayman Islands,Central African Republic,Chad,Chile,China,Colombia,Comoros,Congo,Cook Islands,Costa Rica,Croatia,Cuba,Cyprus,Czech Republic,Denmark,Djibouti,Dominica,Dominican Republic,Ecuador,Egypt,El Salvador,Equatorial Guinea,Eritrea,Estonia,Ethiopia,Falkland Islands,Faroe Islands,Fiji,Finland,France,French Guiana,French Polynesia,Gabon,Gambia,Georgia,Germany,Ghana,Gibraltar,Greece,Greenland,Grenada,Guadeloupe,Guatemala,Guinea,Guinea Bissau,Guyana,Haiti,Honduras,Hong Kong,Hungary,Iceland,India,Indonesia,Iran,Iraq,Ireland,Israel,Italy,Ivory Coast,Jamaica,Japan,Jordan,Kazakhstan,Kenya,Kiribati,Kuwait,Kyrgyzstan,Laos,Latvia,Lebanon,Lesotho,Liberia,Libya,Liechnstein,Lithuania,Luxem bourg,Macau,Macedonia,Madagascar,Malawi,Malaysia,Maldives,Mali,Malta,Marshall Islands,Martinique,Mauritania,Mauritius,Mexico,Micronesia,Moldova,Monaco,Mongolia,Montserrat,Morocco,Mozambique,Myanmar,Namibia,Nepal,Nether lands,Netherlands Antilles,New Caledonia,New Zealand,Nicaragua,Niger,Nigeria,Norfolk Island,North Korea,Northern Mariana Islands,Norway,Oman,Pakistan,Palau,Panama,Papua New Guinea,Paraguay,Peru,Philippines,Poland,Portugal,Puerto Rico,Qatar,Reunion,Romania,Russia,Rwanda,Saint Helena,Saint Kitts and Nevis,Saint Lucia,Saint Pierre and Miquelon,Saint Vincent/Grenadines,Samoa,San Marino,Saotome and Principe,Saudi Arabia,Senegal,Seychelles,Sierra Leone,Singapore,Slovak Republic,Slovenia,Solomon Islands,Somalia,South Africa,South Korea,Spain,Sri Lanka,Sudan,Suriname,Swaziland,Sweden,Switzerland,Syria,Tahiti,Taiwan,Tajikistan,Tanzania,Thailand,Togo,Tokelau,Tonga,Trinidad and Tobago,Tunisia,Turkey,Turkmenistan,Turks and Caicos Islands,Uganda,Ukraine,United Arab Emirates,United Kingdom,United States of America,US Virgin Islands,Uruguay,Uzbekistan,Vanuatu,Vatican,Venezuela,Vietnam,Western Sahara,Yemen,Yugoslavia,Zambia,Zimbabwe',

------------------------>
Note in both cases the length of Australia is 9 characters.

I put in the debug line you suggested (print length($rec{'Country'}) Wink and here's what I get for 11 records
22 16 16 16 16 16 16 16 16 16 16

Now in the db file -- I checked and there are 11 records with the country field of Australia and they are all exactly 9 characters in length.

So how can I get the above lengths 22 16 ...?

Needless to say, it isn't working.

Any ideas?
Quote Reply
Re: Conditional Statements in sub html_record In reply to
It's at least nice to know that my debugging has lead us somewhere!

I don't know where your other characters are coming from.

Oh, wait a minute. Yes I do. (At least I think I do.) Do you have $db_bold set to 1 in your .cfg file? If you've done a search for "Australia" your $rec{'Country'} is going to be "<B>Australia</B>" by the time it gets to html_record. There's your 16 characters.

To fix that, before you print out your link, add a line

$rec{'Country'} =~ s/<?/B>//g;

That will remove the bold tags from the field.

I'm not sure where the length of 22 came from, though.

You might try, instead of the debugging line that I gave you before -- print length($rec{'Country'}); -- use

print "--$rec{'Country'}--";

Then take a look at your page source in your browser and see what the characters are between the dashes.

I'm pretty sure that taking out the bold tags will take care of the lengths that are 16. I'm not sure about the length that is 22, though. It may be that we'll need some more debugging to see what those characters are, if it isn't obvious by looking at the page source.



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





Quote Reply
Re: Conditional Statements in sub html_record In reply to
Oops! Smile

I guess there's not an easy way to check to make sure that your users don't do an "end run" around all your work. It may be possible, though, and we can pursue it if you think it might be a problem again.


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