darkfreaks Posted September 27, 2007 Share Posted September 27, 2007 ok when your first see it it says division by zero on line 14 which is <?php $total=$weight/$heightwo *703; ?> also when it divides i get something like "Your Body Mass Index is 9097.64705882%" i want to format the number so the dot is the second digit to the left like 00.00% Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/ Share on other sites More sharing options...
darkfreaks Posted September 27, 2007 Author Share Posted September 27, 2007 okay i fixed the division by zero problem does anyone know how i would use the format_number function to make it 00.00% ? Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356147 Share on other sites More sharing options...
darkfreaks Posted September 27, 2007 Author Share Posted September 27, 2007 can i even do this with this function? ??? Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356152 Share on other sites More sharing options...
darkfreaks Posted September 27, 2007 Author Share Posted September 27, 2007 is there a way i can return the total and if it returns 0.000000% it converts it to 00% ??? Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356159 Share on other sites More sharing options...
AndyB Posted September 27, 2007 Share Posted September 27, 2007 http://ca.php.net/manual/en/function.sprintf.php Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356160 Share on other sites More sharing options...
darkfreaks Posted September 27, 2007 Author Share Posted September 27, 2007 ok so now i got sprintf ('01.2f',$total); and it just displays blank? ??? Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356168 Share on other sites More sharing options...
darkfreaks Posted September 27, 2007 Author Share Posted September 27, 2007 could someone paste aan example as how i would do it via sprintf? Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356175 Share on other sites More sharing options...
trq Posted September 27, 2007 Share Posted September 27, 2007 If you want it echo'd at the same time you need to use printf(). printf ('%01.2f',$total); Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356176 Share on other sites More sharing options...
darkfreaks Posted September 27, 2007 Author Share Posted September 27, 2007 now i get 2166.00% how would i make it so it is like 00.00% Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-356183 Share on other sites More sharing options...
AndyB Posted September 29, 2007 Share Posted September 29, 2007 now i get 2166.00% how would i make it so it is like 00.00% Do you mean how would you get that example value to display as 21.66%? If so, try the old divide by 100 trick. Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358121 Share on other sites More sharing options...
darkfreaks Posted September 29, 2007 Author Share Posted September 29, 2007 my entire code is below i want it to calculate Body Mass Index Formula if someone can look one up and see if i have done my math correctly? <?php $height= trim($_POST['height']); $weight=trim($_POST['weight']); $height=str_replace(',','',$height); $height=str_replace('.','',$height); $height=strip_tags($height); $weight=strip_tags($weight); $weight=str_replace(',','',$weight); $weight=str_replace('.','',$weight); if ($_POST['height']||$_POST['weight']=="true") { $weighttwo=$weight*100; $total=$weighttwo/$height;} if($_POST['weight']=="") {$error['weight']="Please enter a weight!";} else {unset($error['weight']);} if($_POST['height']=="") {$error['height']="Please enter a height!";} else {unset($error['height']);} if($error) {?> <form method="POST"> <b>Height(inches):</b><input name="height" type="text" size="3"><?php if ($_POST) { echo $error['height'];} ?><b>Weight(lbs.):</b><input name="weight" type="text" size="3"><?php if ($_POST) { echo $error['weight'];} ?> <input type="submit" name="Calculate!" value="Calculate!"> </form> <?php } else{echo "Your Mass Body Index is"; echo $total; echo "%"; } ?> Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358124 Share on other sites More sharing options...
darkfreaks Posted September 29, 2007 Author Share Posted September 29, 2007 nvm the formula is all messed up its sposed to be $weight/$height*$height*703 Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358125 Share on other sites More sharing options...
darkfreaks Posted September 29, 2007 Author Share Posted September 29, 2007 now i get Your Mass Body Index is160284% i think im still doing something wrong the forumula is BMI = ( Weight in Pounds / ( Height in inches ) x ( Height in inches ) ) x 703 Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358128 Share on other sites More sharing options...
AndyB Posted September 29, 2007 Share Posted September 29, 2007 I suspect the real formula is: BMI = (weight in pounds * 703) / (height in inches * height in inches) Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358130 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 ok so i put $bmi= ($weight*703)/($height*2); and now its getting abit morre realistic i get your BMi is 1083% i want to knock it down to a 2 digit whole so its like 1-50 percent and even then i dont think its adding up right or something because it would be 10.83 percent and at my current weight and height it should come out to 29.9 percent Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358132 Share on other sites More sharing options...
AndyB Posted September 30, 2007 Share Posted September 30, 2007 No, not height times two. height * height. Basic math Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358136 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 ok its working now my fault now i get 29.2712345 how do i get rid of the .stuff? Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358138 Share on other sites More sharing options...
Barand Posted September 30, 2007 Share Posted September 30, 2007 printf(); Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358139 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 ok so i did printf($total); and i still see no diff? Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358140 Share on other sites More sharing options...
AndyB Posted September 30, 2007 Share Posted September 30, 2007 Try again. Take a few minutes to re-read the useful contributions in this thread. Take a deep breath. All the help you need is in this thread. Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358143 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 thanks to you all this thread will be closed in 2 seconds Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358148 Share on other sites More sharing options...
neylitalo Posted September 30, 2007 Share Posted September 30, 2007 ok so i put $bmi= ($weight*703)/($height*2); and now its getting abit morre realistic i get your BMi is 1083% i want to knock it down to a 2 digit whole so its like 1-50 percent and even then i dont think its adding up right or something because it would be 10.83 percent and at my current weight and height it should come out to 29.9 percent If you'll notice, AndyB didn't say "height * 2", he said "height * height". There's a rather significant difference there. Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358150 Share on other sites More sharing options...
Barand Posted September 30, 2007 Share Posted September 30, 2007 If you'll notice, AndyB didn't say "height * 2", he said "height * height". There's a rather significant difference there. Only if height != 2 Link to comment https://forums.phpfreaks.com/topic/70840-solved-few-probs-with-bmi-calculator/#findComment-358151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.