darkfreaks Posted October 1, 2007 Share Posted October 1, 2007 ok i need a way to return the right output but it only returns true until you get to normal weight, everything after that returns false so if your over weight or obese it just says your normal weight and no one so far can rectify the problem. <?php } elseif ($total<18.50){ print"Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; print "<b>You are Currently Underweight This is not healthy</b>";} else if ($total>18.50){print "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Normal Weight, give yourself a pat on the back!</b>";} elseif ($total<25){print "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Normal Weight, give yourself a pat on the back!</b>";} else if ($total>25){echo "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} elseif ($total<30){print "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} elseif ($total>30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor ASAP!</b>";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/ Share on other sites More sharing options...
darkfreaks Posted October 1, 2007 Author Share Posted October 1, 2007 any ideas? ??? Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359447 Share on other sites More sharing options...
LemonInflux Posted October 1, 2007 Share Posted October 1, 2007 Hm. So the first one works, and no other ones do? Also, the first }; is this only part of the code? Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359450 Share on other sites More sharing options...
darkfreaks Posted October 1, 2007 Author Share Posted October 1, 2007 well before the html form i have an if ($error) { ?> statement Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359453 Share on other sites More sharing options...
LemonInflux Posted October 1, 2007 Share Posted October 1, 2007 Hmm...I can't see a difference between each section...It's a problem with the if's, I'm fairly sure. But I can't find it. Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359456 Share on other sites More sharing options...
BlueSkyIS Posted October 1, 2007 Share Posted October 1, 2007 yow, mang, fix up your code so we can read it. if ($total<18.50){ print"Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; print "<b>You are Currently Underweight This is not healthy</b>"; } else if ($total>18.50) { // THIS IS ALWAYS TRUE unless $total == 18.50. One fix is to start at the lowest number and use < upward: if ($total <18.50) { // do something } else if ($total <= 25) { // do something } else if ($total <= 30) { // do something } else { // Total is > 30 // do something } Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359457 Share on other sites More sharing options...
darkfreaks Posted October 1, 2007 Author Share Posted October 1, 2007 the problemat lies with the following statements as underweight and normal weight return true <?php elseif ($total<30){print "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} elseif ($total>30){print "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor. </b>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359464 Share on other sites More sharing options...
The Little Guy Posted October 1, 2007 Share Posted October 1, 2007 change them all to if, not else if, and see if that does it. Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359465 Share on other sites More sharing options...
darkfreaks Posted October 1, 2007 Author Share Posted October 1, 2007 i tried that and the problem with that is it echos on submit every single statement Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359469 Share on other sites More sharing options...
BlueSkyIS Posted October 1, 2007 Share Posted October 1, 2007 ah come on guys/gals. follow the logic if ($total < 18.50) { // The total is LESS THAN 18.5 // But it's not so... } else if ($total > 18.50) { // It's not less than 18.5, right? Therefore it is either equal to or > 18.50. Assuming it's not = 18.5, then it is greater than 18.5, therefore this IF is true. No?? The exact same problem is encountered if $total is > 30. You need to either nest your IF's (lenghty) or use an incremental if as I stated before: if ($total <18.50) { // do something } else if ($total <= 25) { // do something } else if ($total <= 30) { // do something } else { // Total is > 30 // do something } Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359470 Share on other sites More sharing options...
BlueSkyIS Posted October 1, 2007 Share Posted October 1, 2007 here, to replace your first code: <? } elseif ($total<18.50) { print"Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; print "<b>You are Currently Underweight This is not healthy</b>"; } else if ($total < 25) { print "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Normal Weight, give yourself a pat on the back!</b>"; } elseif ($total< 30) { print "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>"; } else { echo "Your Body Mass Index is"; printf ('%01.2f',$total); print "%"; print "<b>You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor ASAP!</b>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359478 Share on other sites More sharing options...
darkfreaks Posted October 1, 2007 Author Share Posted October 1, 2007 now i get Warning: Division by zero in /home/lilysgra/public_html/BMIcalc.php on line 10 Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359500 Share on other sites More sharing options...
darkfreaks Posted October 1, 2007 Author Share Posted October 1, 2007 fixed! i figured out that the max allowed else if's is 3 by dafault so i did 3 of those and an else. works now perfect thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/71410-solved-help-with-script/#findComment-359548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.