Jump to content

Recommended Posts

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>";}


?>




Link to comment
https://forums.phpfreaks.com/topic/71410-solved-help-with-script/
Share on other sites

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
}

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>"; ?>

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

}

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>";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.