darkfreaks Posted September 30, 2007 Share Posted September 30, 2007 okay so everything should work it echos right for underweight and normal weight but if i go over 30 percent it echos you are normal weight. when it should echo you are obese please consort a doctor. <?php } else if ($total<18.50){ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Currently Underweight This is not healthy</b>";} else if ($total>18.50){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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); echo "%"; echo "<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); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total<30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total>30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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/71219-else-if-problems/ Share on other sites More sharing options...
KrisNz Posted September 30, 2007 Share Posted September 30, 2007 Well if your $total is 100 the first condition thats going to be met is that $total is > than 18.5. The other conditions wont get a look in. You want your conditions to be greater than a and less than b. Also doing this as a switch{} would be a bit cleaner. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358225 Share on other sites More sharing options...
jd2007 Posted September 30, 2007 Share Posted September 30, 2007 use switch....who can read that ? Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358226 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 nevermind that echos everything gah! Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358227 Share on other sites More sharing options...
jbingman Posted September 30, 2007 Share Posted September 30, 2007 Your problem is a math error. Say you run the number 100 through the statements. it will run through the first 2 but on the 3rd (>25) it returns true. So if the number is over 25 AND 30 it will run through the first one on the list. You would need to add a domain such as 25<x<30 (number is greater than 25 but less than 30) then x<30 <?php } else if ($total<18.50){ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Currently Underweight This is not healthy</b>";} else if ($total>18.50 && $total<25){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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); echo "%"; echo "<b>You are Normal Weight, give yourself a pat on the back!</b>";} else if ($total>25 && $total<30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total<30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total>30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor ASAP!</b>";} ?> I hope that works and makes sense Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358230 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 indeed i did have a few math errors but i think it is only looping through the first statement then it doesnt return anything else. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358236 Share on other sites More sharing options...
jbingman Posted September 30, 2007 Share Posted September 30, 2007 Yeah so try to play with the domains as I did above to only include a set of numbers that only that else if statement contains. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358237 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 my current code is <?php } else if ($total<18.50){ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Currently Underweight This is not healthy</b>";} else if ($total>18.50){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Normal Weight, give yourself a pat on the back!</b>";} else if ($total<25||$total>25){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Normal Weight, give yourself a pat on the back!</b>";} else if ($total<30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total>30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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/71219-else-if-problems/#findComment-358240 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 oh i see why its flawed Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358241 Share on other sites More sharing options...
jbingman Posted September 30, 2007 Share Posted September 30, 2007 Yeah you got the idea. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358242 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 gah i fixed that problem am i the only one that is thinking that only the first else if is returning true? Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358244 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 okay so i fixed up the code using switch but im getting erros can someone fix them plz? <?php switch ($total) { case $total>18.50: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Normal Weight, give yourself a pat on the back!</b>"; break; case $total<18.50: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Currently Underweight This is not healthy</b>"; break; case $total<25: echo Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Normal Weight, give yourself a pat on the back!</b>"; break; case $total>25: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>"; break; case $total>30: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor ASAP!</b>"; break; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358248 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358253 Share on other sites More sharing options...
Ninjakreborn Posted September 30, 2007 Share Posted September 30, 2007 <?php switch ($total) { case $total>18.50: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Normal Weight, give yourself a pat on the back!"; break; case $total<18.50: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Currently Underweight This is not healthy"; break; case $total<25: echo Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Normal Weight, give yourself a pat on the back!"; break; case $total>25: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Over Weight, Get to Exercizing on a Daily Basis!"; break; case $total>30: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor ASAP!"; break; } ?> Try that, if not paste your EXACT errors. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358255 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 ok small syntax error but works now only problem is i get Your Body Mass Index is0.00%You are Normal Weight, give yourself a pat on the back! and it shouldnt echo there is nothing filled in yet Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358258 Share on other sites More sharing options...
Ninjakreborn Posted September 30, 2007 Share Posted September 30, 2007 paste the syntax errors sent to the browser, or the output. Not the code.. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358260 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 Your Body Mass Index is0.00%You are Normal Weight, give yourself a pat on the back! need to know how to make that stop echoing if nothing is filled in. also shouldt it be saying you are under weight since it equals nothing? Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358261 Share on other sites More sharing options...
jbingman Posted September 30, 2007 Share Posted September 30, 2007 make a default value in the switch statement for 0. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358262 Share on other sites More sharing options...
jbingman Posted September 30, 2007 Share Posted September 30, 2007 Your Body Mass Index is0.00%You are Normal Weight, give yourself a pat on the back! need to know how to make that stop echoing if nothing is filled in. also shouldt it be saying you are under weight since it equals nothing? go herehttp://www.w3schools.com/php/php_switch.asp it will show you how to use the default action in the switch statement and explain it. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358265 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 okay set the default value so it doesnt echo at all. but now im still getting your BMI is 29.9 percent your a normal weight when its sposed to echo your overweight please exercise should i get rid of the if else statements and leave the switch part? Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358271 Share on other sites More sharing options...
jbingman Posted September 30, 2007 Share Posted September 30, 2007 Yes, just keep the switch statement. Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358274 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 yeah i noticed it appeared blank so i put it back perhaps my statements are more flawed than i originally thought? <?php } else if ($total<18.50){ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Currently Underweight This is not healthy</b>";} else if ($total>18.50){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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); echo "%"; echo "<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); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total<30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total>30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor ASAP!</b>";} switch ($total) { case $total=0: break; case $total>18.50: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Normal Weight, give yourself a pat on the back!"; break; case $total<18.50: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Currently Underweight This is not healthy"; break; case $total<25: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Normal Weight, give yourself a pat on the back!"; break; case $total>25: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Over Weight, Get to Exercizing on a Daily Basis!"; break; case $total>30: echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "You are Over Weight, Get to Exercizing on a Daily Basis! Consult a Doctor ASAP!"; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358276 Share on other sites More sharing options...
teng84 Posted September 30, 2007 Share Posted September 30, 2007 <?php if(i dont know where to start condition)//just put here yours } else if ($total<18.50){ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Currently Underweight This is not healthy</b>"; } else if ($total<25){ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Normal Weight, give yourself a pat on the back!</b>"; } else if ($total<30){ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Normal Weight, give yourself a pat on the back!</b>"; } else{ echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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/71219-else-if-problems/#findComment-358279 Share on other sites More sharing options...
darkfreaks Posted September 30, 2007 Author Share Posted September 30, 2007 ok my problem is the following statement wont work i have 5 if else statements that work if i change them to if and if else statements it echos everything upon submit instead of doing the math and figuring otu what to echo. the last 4 statements that dont work right: <?php else if ($total<25){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total<30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<b>You are Over Weight, Get to Exercizing on a Daily Basis!</b>";} else if ($total>30){echo "Your Body Mass Index is"; printf ('%01.2f',$total); echo "%"; echo "<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/71219-else-if-problems/#findComment-358513 Share on other sites More sharing options...
darkfreaks Posted October 1, 2007 Author Share Posted October 1, 2007 anyone got any solutions for all my else ifs? if i change it to if() elseif() elseif() it echos everything upon submit? Quote Link to comment https://forums.phpfreaks.com/topic/71219-else-if-problems/#findComment-358843 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.