Click to See Complete Forum and Search --> : Need help with MATH function.


thunder77
12-05-2005, 06:44 PM
I'm looking to code this http://www.cdc.gov/nccdphp/dnpa/bmi/calc-bmi.htm in php... You can see the javascript if you view the source. This isn't stealing, right? It's a standard formula, I just figured this was the easiest way to show it... ANY help would be GREATLY appreciated. I don't know math functions...

bathurst_guy
12-05-2005, 06:48 PM
<html>
<head> blah blah...

<?
if(isset($_POST[submit])){
switch($_POST[measurement]){
case "english":
# English
if(!isset($_POST[inches])) $_POST[inches] = 0;
$totalinches = $_POST[feet] * 12 + $_POST[inches];
$englishvalue = ($_POST[pounds] * 703 * 10 / $totalinches / $totalinches) / 10;
break;
case "metric":
#Metric
$metricvalue = ($_POST[kilograms] * 10 / $_POST[meters] / $_POST[meters]) / 10;
break;
}
}else{
$englishvalue = "";
$metricvalue = "";
}
?>
<h1>English Measurement</h1>
<form name="english" action="<?=$_SERVER[php_self]?>" method="post">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td align="right">Height:&nbsp;</td>
<td> <input type="TEXT" name="feet" size="5">
feet</td>
</tr>

<tr>
<td align="right">and&nbsp;</td>
<td> <input type="TEXT" name="inches" size="5"> inch(es)</td>
</tr>
<tr>
<td align="right">Weight:&nbsp;</td>

<td> <input type="TEXT" name="pounds" size="5"> pounds</td>
</tr>
<tr>
<td colspan="2" class="psmall">(Note: 8 ounces = 0.5 pounds) </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value=" Calculate "><input type="hidden" name="measurement" value="english"><p>

Your BMI: <input type="TEXT" name="calcval" size="10" value="<?=$englishvalue?>"></td>
</tr>
</table>
</form>
<h1>Metric Measurement</h1>
<form name="metric" action="<?=$_SERVER[php_self]?>" method="post">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>

<td align="right">Height:&nbsp;</td>
<td><input type="TEXT" name="meters" size="5">
Meters</td>
</tr>
<tr>
<td colspan="2" class="psmall">(Note: 1 Meter = 100cm)</td>
</tr>

<tr>
<td align="right">Weight:&nbsp;</td>
<td><input type="TEXT" name="kilograms" size="5"> Kilograms</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value=" Calculate "><input type="hidden" name="measurement" value="metric">
<p>Your BMI: <input type="TEXT" name="calcval" size="10" value="<?=$metricvalue?>"></td>

</tr>
</table>
</form>

LiLcRaZyFuZzY
12-05-2005, 07:02 PM
<?php
# int get_bmi(int weight, int height, bool metric, bool centimeters)
function get_bmi($weight, $height, $metric = true, $centimeters = false){
if($metric){
# metric formula
$const = 1;
if($centimeters){
$const = 10000;
}
}else{
#english formula
$const = 703;
}
$bmi = $weight / (($height)*($height));
$bmi *= $const;

return round($bmi, 1);
}

# 57kg / 1.64 meter -> will return 21.2
echo get_bmi(57,1.64, true, false);
?>

ShrineDesigns
12-05-2005, 08:57 PM
lol, easy formula<?php
function m2cm($meters)
{
return $meters * 100;
}
function ft2in($feet)
{
return $feet * 12;
}
function bmi($weight, $height, $metric = 0)
{
if(!$metric)
{
return ($weight / pow($height, 2)) * 703;
}
return ($weight / pow($height, 2)) * 10000;
}
?>

thunder77
12-06-2005, 02:51 PM
Great. Thanks guys, and I knew it was easy since I mentioned it on my post. I just don't know ANY of the math functions and do not have time to learn them for this quick fix.

Here's another quick question that should be easily solved. If I want to pass on the result to another "results" page do I just use my POST and extract it on the following page?

Many thanks...again.

Sheldon
12-06-2005, 02:59 PM
yer or sessions

thunder77
12-06-2005, 03:07 PM
Also, how can I round off the result to whole numbers only?

LiLcRaZyFuZzY
12-06-2005, 03:30 PM
replace 1 by 0 (second argument) in the round() function

return round($bmi, 0);

ShrineDesigns
12-06-2005, 04:47 PM
you could also type cast it to an integer, example$n = 1.5;
echo (int) $n; // 2
// or
echo floor($n); // 1
echo ceil($n); // 2

ZEMPCO
01-03-2007, 11:29 PM
Howdy,

You guys gave me some info I used as well. I didn't know much about the math functions. In mys script though, how do I force the decimals for currency? If the result is even it doesn't show the currency.

($_POST['price']*.08)+($_POST['price'])

NightShift58
01-04-2007, 12:00 AM
You can use:<?php
print number_format(1000 * 1.08, 2 );
// output: 1,080.00

print number_format(1000 * 1.08, 2, ",", "." );
// output: 1.080,00
?>see: http://us2.php.net/manual/en/function.number-format.php

Sheldon
01-04-2007, 12:11 AM
http://php.net/number_format

is this what are you trying to do?