Gossamer Forum
Home : Products : DBMan : Installation :

Metric conversion

Quote Reply
Metric conversion
I have created a text input field Length1, which the length is entered into and depending on the radio button pressed Meters/Feet stored in Length2 it will calculate the other value to one decimal place. Any idea's or suggestions would be greatly appreciated.
Quote Reply
Re: Metric conversion In reply to
So you have one field where people can enter either feet or meters and a radio button that says which it is. Right? Then you want to do a conversion and end up with that same field to be the feet and the another field to be the meters? (Just getting it straight before we go any further. Smile )

If all that's right, I'm gonna assign some names to the fields so I can keep them straight.

I'll call the first field where they enter the data Feet, the radio buttons will be Unit and the other field that holds the meters will be (not surprisingly) Meters. The options for the Unit radio field are feet and meters. Change the code below to match your own fields.

You'll need to add the following code to sub add_record just after

($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);

You may need to add it to sub modify_record, too. You would add it just after

my ($status, $line, @lines, @data, $output, $found, $restricted);

I haven't tried this exactly, but I've done other things and it should work.

Code:
if ($in{'Feet'}) {
if ($in{'Unit'} eq "feet") {
$in{'Meters'} = ($in{'Feet'} * .3048);
$in{'Meters'} = sprintf("%.1f",$in{'Meters'});
}
else ($in{'Unit'} eq "meters") {
$in{'Meters'} = $in{'Feet'};
$in{'Feet'} = ($in{'Feet') / .3048);
$in{'Feet'} = sprintf("%.1f",$in{'Feet'});
}
}

(I looked up the conversion factor. Please check it to be sure.)

It probably would be best to have one of the options in the radio field checked by default so you are sure that you'll have one or the other checked. You don't want it to be blank.

The rounding I gave you above will print out a ".0" if there is no digit in the decimal place. If you need to get rid of that, I'll go look and see if I can find how to.


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





Quote Reply
Re: Metric conversion In reply to
Thanks JPD for your fast reply, unfortunatly I did as you said and recieved a 500 error. I changed the fields in html.pl, however this doesn't matter because the db.cgi script isn't running.This is what I used in
the two sub's as directed.

if ($in{'Feet'}){
if ($in{'Unit'} eq "feet"){
$in{'Meters'} = ($in{'Feet'} * .3048);
$in{'Meters'} = sprintf("%.1f",$in{'Meters'});
}
else ($in{'Unit'} eq "meters"){
$in{'Meters'} = $in{'Feet'};
$in{'Feet'} = ($in{'Feet') / .3048);
$in{'Feet'} = sprintf("%.1f",$in{'Feet'});
}
}

Any idea what's not working, all the brackets match up, I'm lost.


Quote Reply
Re: Metric conversion In reply to
Sorry 'bout that. Two typos on my part. One of these days I'm going to remember to check the code for syntax errors before I post it here. This is the correct code:

Code:
if ($in{'Feet'}) {
if ($in{'Unit'} eq "feet") {
$in{'Meters'} = ($in{'Feet'} * .3048);
$in{'Meters'} = sprintf("%.1f",$in{'Meters'});
}
else {
$in{'Meters'} = $in{'Feet'};
$in{'Feet'} = ($in{'Feet'} / .3048);
$in{'Feet'} = sprintf("%.1f",$in{'Feet'});
}
}

Again, sorry.

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




[This message has been edited by JPDeni (edited May 04, 1999).]
Quote Reply
Re: Metric conversion In reply to
Success!! You a champ JPD. Again thanks for your fast reply and sharp mind.
Quote Reply
Re: Metric conversion In reply to
Whoops... trying to apply a mod to the wrong program!

[This message has been edited by oldmoney (edited May 23, 1999).]