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)

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/

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!