Page 1 of 1

Creating a BMI calculator

Posted: Tue Nov 06, 2007 4:56 pm
by ENJN
Hi,

I'm new to Joomla and PHP. I'm still learning a lot of stuff and was hoping to get some help.

I'd like to add a BMI (Body Mass Index) calculator to my site. The formula is very easy, it's weight (kg) / height (m) ^2.

I've gone through the steps of the Hello World module maker to figure out how to add the php code to the site. But I'm running into problems and I'd like to get some help.

Code: Select all

<?php
/**
* @version 1.0 $
* @package BMI
* @copyright (C) 2007 Me
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/
 
/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
 
?>

<form action="<?php echo sefRelToAbs( 'index.php?option=com_content&task=view&id=919' ); ?>" method="post">
<table summary="bmi" border="0" cellpadding="8" cellspacing="8" height="155" width="144">
  <tbody>
   <tr>
    <td>
    <p>
    Height (cm):
    </p>
    </td>
    <td><input name="height" size="2" type="text" /></td>
   </tr>
   <tr>
    <td>
    <p>
    Weight (kg):
    </p>
    </td>
    <td><input name="weight" size="2" type="text" /></td>
   </tr>
   <tr>
    <td colspan="2" align="center">
    <input value="Submit" type="submit" /><br />
    </td>
   </tr>
  </tbody>
</table>
</form>

<?php

$weight = $_POST['weight'] ;
$height = $_POST['height'] ;
?>
<?php
if ($height > 1) {

$heightm = $height / 100 ;

$bmi = $weight / ($heightm * $heightm);
$bmi = sprintf("%01.1f", $bmi);
}
?>
My BMI is: <?php if ($height > 1) { echo " $bmi " ; } ?>
This causes the BMI to appear in the bottom of the module and also takes you to a page which explains what the BMI means. I'm wondering if there's a way to make it so that after you submit, instead of the module showing the same "Enter the height and weight" stuff as before, it displays just the number.

Alternatively, it'd be great if there was a way to get the BMI number to appear in the static content page explaining the BMI.