Body Mass Index – Behind the Scenes

Originally for my post on the Body Mass Index (BMI), I wanted to include a BMI calculator directly in the post.  I found a WordPress plugin, PHP Code for Posts that allowed me to use PHP code in the post and coded the calculator.  Unfortunately, when I tried to put the calculator on the page,  I ran into an issue with how the variables are passed from the form to the function that handles the calculations.  The code works on it’s own, but embedded in a post it fails.  I ended up making a link to the URL where I had tested the calculator.  I’m hoping to find a better solution in the future.

Here’s the code, in case you’re intersted (formatted with Preserve Code Formatting). Normally, I’d pull the style sheet and HTML into their own files, but for the purposes of displaying the code I have them all together.

 



Body Mass Index (BMI) Calculator for Adults

'; function bmiForm() { echo '



'; if ((empty($weight)) && ($_POST['done'] == 'y') || (empty($feet)) && ($_POST['done'] == 'y') || (empty($inches)) && ($_POST['done'] == 'y')) echo '

Please complete all fields above.

'; } function bmiResult() { global $weight, $height, $feet, $inches; $BMI = number_format((($weight / ($height * $height)) * 703), 1, '.', ''); echo '

Your Body Mass Index (BMI) is: ' .$BMI. '

'; echo '

Based upon a height of ' .$feet. ' feet, ' .$inches. ' inches and a weight of ' .$weight. ' pounds

'; if ($BMI <= 18.5) { echo '

Your BMI falls in the "Underweight" category.

Understanding your BMI: BMI is not intended to diagnose a problem, but to indicate when follow up with a healthcare professional is a good ideas. It is possible for a person to be healthy with a BMI that falls outside of the Normal range. However, you should consider seeking the opinion of a healthcare professional or registered dietitian.

'; } elseif (($BMI > 18.5) && ($BMI <= 24.9)) { echo '

Your weight is normal.

Understanding your BMI: BMI is not intended to diagnose a problem, but to indicate when follow up with a healthcare professional is a good ideas. A BMI within the Normal range is associated with reduced risk of diseases, including diabetes and high blood pressure, which are more common in overweight and obese individuals.

'; } elseif (($BMI >= 25) && ($BMI <= 29.9)) { echo '

Your BMI falls in the Overweight category.

Understanding your BMI: BMI is not intended to diagnose a problem, but to indicate when follow up with a healthcare professional is a good ideas. A BMI within the Overweight range is associated with increased risk of disease, including diabetes and high blood pressure. While it is possible to be healthy with this BMI, you should consider seeking the opinion of a healthcare professional or registered dietitian.

'; } elseif ($BMI >= 30) { echo '

Your BMI falls in the Obese category.

Understanding your BMI: BMI is not intended to diagnose a problem, but to indicate when follow up with a healthcare professional is a good ideas. A BMI within the Obese range is associated with increased risk of disease, including diabetes and high blood pressure. While it is possible to be healthy with this BMI, you should consider seeking the opinion of a healthcare professional or registered dietitian.

'; } else { echo "

Sorry, something went wrong

"; } } if ((isset($_POST['done'])) && ($_POST['done'] == 'y') && ($_POST['inches'] != NULL) && ($_POST['weight'] != NULL) && ($_POST['feet'] != NULL)) { bmiResult(); } else { bmiForm(); } echo '

This calculator is intended for use by individuals over 20 years of age. More Information about BMI from the Centers for Disease Control and Prevention

'; ?>