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: [Damon] US to metric or metric to US conversions on the fly?

By Codee - June 21, 2011

Hi Damon and anyone else that can help,

I am a neophyte at php stuff. I substituted a data field called "testcm" (which is the person's height in centimeters in the database) and the function does calculate...but how do I echo/print the code onto a cmsb details php page, and without the function showing...just the results? Here's my initial try:

<?php if ($modelsRecord['testcm']): ?>
.function convert_height($<?php echo $modelsRecord['testcm'] ?>) {
$inch = round($<?php echo $modelsRecord['testcm'] ?> * 0.3937);
$ft = floor($inch / 12);
$inch = ($inch % 12);
return $ft . "' " . $inch . '"';
}
<?php endif ?>

Thanks for any help!

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/