US to metric or metric to US conversions on the fly?

6 posts by 2 authors in: Forums > CMS Builder
Last Post: June 21, 2011   (RSS)

By Codee - June 20, 2011

Has anyone done the following:

1) CMSB Admin User puts data into a field, like 185.42 into a cm field.
2) On the public display php page it shows as "185.42cm" but also shows "6ft 1in".

So the php code converts the entry on the backend displays on the front end as both metric and the us conversion.

My client has a lot of files so it's in her best interest to just enter into the cm field one time which will save her a lot of time over the long haul. Right now she would have to convert each one manually and enter each one manually. There would also be less mistakes and faster corrections if it were more automated.

Thank you in advance!

Re: [equinox69] US to metric or metric to US conversions on the fly?

By Damon - June 21, 2011

Hi,

Take a look at this PHP code:
http://forrst.com/posts/Convert_Height_Centimeters_to_Feets_and_Inches-gs6

I think it will do exactly what you need.

Hope that helps!
Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Damon] US to metric or metric to US conversions on the fly?

By Codee - June 21, 2011

Great find Damon! It does look exactly like what I need! Thanks!

Re: [equinox69] US to metric or metric to US conversions on the fly?

By Damon - June 21, 2011

Hi,

Keep the function code seperate from the publish code.

So, put this code at the top of your page:

<?php
// convert inches into feet

function convert_height($height) {
$inch = round($height * 0.3937);
$ft = floor($inch / 12);
$inch = ($inch % 12);
return $ft . " feet " . $inch . ' inches';
}
?>


Next, in your publish code try something like this:
<?php if ($modelsRecord['testcm']): ?>
<?php echo convert_height($modelsRecord['testcm']); ?>
<?php endif ?>



That should do. :)
Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Damon] US to metric or metric to US conversions on the fly?

By Codee - June 21, 2011 - edited: June 22, 2011

This is working but one small glitch needs a repair:

here's the code I'm using:
<?php // convert inches into feet
function convert_height($height) {
$inch = round($height * 1.00);
$ft = floor($inch / 12);
$inch = ($inch % 12);
return $ft . " &#8217; " . $inch . ' &#8221;';
}
?>


I'd like a data entry of 67.25 inches, which currently displays as 5' 7" to actually display 5' 7.25". I've tried about 40 different approaches including round with precision=2...but I'm not getting it to occur.

Any help is appreciated!