TimUSA Posted January 8, 2008 Share Posted January 8, 2008 can someone tell me whats wrong here? //DISPLAY ERROR MESSAGES if (isset($_POST['boats'] < "2")) { echo "THE NUMBER OF BOATS YOU ENTERED IS INCORRECT"; } if (isset($_POST['boats'] = "2" and $_POST['factor'] != "2")) { echo "THE NUMBER OF BOATS YOU ENTERED IS INCORRECT"; } if (isset($_POST['boats'] != "2" and $_POST['factor'] = "2")) { echo "THE NUMBER OF BOATS YOU ENTERED IS INCORRECT"; } Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/ Share on other sites More sharing options...
interpim Posted January 8, 2008 Share Posted January 8, 2008 try assigning your values from POST to variables and echo those out to see what they contain to help with the troubleshooting. There is a difference between "2" and your checking if the value in your post is less than "2" not the numerical 2. Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433828 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 remove references to isset In your IF statement, use == instead of = if it's equal Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433830 Share on other sites More sharing options...
dsaba Posted January 8, 2008 Share Posted January 8, 2008 try this: if (isset($_POST['boats']) && $_POST['boats'] < 2) { //do some stuff } I assume you don't want to check the value inside of 'boats' is less than 2 unless it has been defined/set. The way an && statement works in php is that it first checks the first part of the && statement, if this first statement is not true then it ignore the rest of the code and continue on. So you won't get parse errors in trying to compare an undefined value of 'boats' never, with a statement like this. Because it checks if it has been set first. Also I noticed you are comparing integer values with the < operator, php will automatically convert your "2" string into the integer 2, save it some work and just use 2 without the quotes since its the same thing in your case. Do notice that 2 != "2" of course for your purposes you're checking numerical value, so 5 < "2" is the same as 5 < 2 as far as php is concerned. Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433838 Share on other sites More sharing options...
TimUSA Posted January 8, 2008 Author Share Posted January 8, 2008 ok everything working except one hang up... //DISPLAY ERROR MESSAGES if (isset($_POST['boats']) && $_POST['boats'] < 2) { echo "THE NUMBER OF BOATS YOU ENTERED IS INCORRECT"; } else if (isset($_POST['boats']) && $POST['factor'] = 2 && $_POST['boats'] <> 2) { echo "THE NUMBER OF BOATS YOU ENTERED IS INCORRECT"; } else { echo "passed"; } this works great: if I enter 1 boat it fails, which it should if I enter 2 boats and a factor of 2 it passes, which it should if I enter 3 boats and a factor of 2 it fails, which it should here is what i am not able to do: if I enter 3 boats and a factor anything other than 2, should = pass if I enter 2 boats and a factor anything other than 2, should = fail I'm sure its a logic thing that i am not getting. Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433887 Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 $_POST['boats'] <> 2 should be: $_POST['boats'] < 2 ? Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433898 Share on other sites More sharing options...
TimUSA Posted January 8, 2008 Author Share Posted January 8, 2008 only problem there is that what if the factor =1 Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433901 Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 What does factor has to do with boats? Then it should say passed. Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433904 Share on other sites More sharing options...
TimUSA Posted January 8, 2008 Author Share Posted January 8, 2008 basically a form is entered where the user selects the number of boats in a race and: 1. Fleet Race - factor = 1 2. Match Race - factor = 2 3. Club Fleet Race - factor = 3 4. Championship - factor = 4 also, 1. you can never have one boat in a race. 2. a match race can only exist between 2 players so i am attempting to validate the form this way: 1. if boats<2 then fail 2. if factor=2 and boats=2 then pass 3. if factor=2 and boats <> 2 then fail 4. if factor<>2 and boats=2 then fail 5. all others pass Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433914 Share on other sites More sharing options...
dsaba Posted January 8, 2008 Share Posted January 8, 2008 if you're checking if something is equal to something else use this symbol: == not = the = symbol assigns a value, it does not compare so change: $POST['factor'] = 2 to: $POST['factor'] == 2 Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433917 Share on other sites More sharing options...
trq Posted January 8, 2008 Share Posted January 8, 2008 so i am attempting to validate the form this way: 1. if boats<2 then fail 2. if factor=2 and boats=2 then pass 3. if factor=2 and boats <> 2 then fail 4. if factor<>2 and boats=2 then fail 5. all others pass If you can write that in plain english then you need to learn the basics of php's comparison operators and write the exact same thing in php. Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433924 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 In your IF statement, use == instead of = if it's equal Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433928 Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 If you can write that in plain english then you need to learn the basics of php's comparison operators and write the exact same thing in php. thorpe makes a brilliant point. Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-433930 Share on other sites More sharing options...
TimUSA Posted January 9, 2008 Author Share Posted January 9, 2008 arrrrffff. Im sorry but I am just not getting this: else if ($_POST['boats'] == 2 && ($POST['factor'] == 1 || $POST['factor'] >= 3)) { echo "THE NUMBER OF BOATS YOU ENTERED IS INCORRECT"; } the way i am reading this is if boats = 2 and (factor = 1 or is >=3) then fail so if i enter 2 and 2 it should pass which it is!!! or if i enter 2 and 3 it should fail, which it is not this is after much reading...arrrrfffffff Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434112 Share on other sites More sharing options...
teng84 Posted January 9, 2008 Share Posted January 9, 2008 2 and 0 false 2 and 2 false 2 and 1 true 2 and >=3 true Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434116 Share on other sites More sharing options...
Stooney Posted January 9, 2008 Share Posted January 9, 2008 From the looks of it you just wanna make sure both boats and factor equals 2. Tell me if I'm wrong. <?php if($_POST['boats']!="2" || $_POST['factor']!="2"){ echo "wrong number of boats"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434123 Share on other sites More sharing options...
TimUSA Posted January 9, 2008 Author Share Posted January 9, 2008 well a bit more than that, yes 2 boats can only be factor 2 however 3++ boats can be anything but 2 Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434155 Share on other sites More sharing options...
Stooney Posted January 9, 2008 Share Posted January 9, 2008 If both boats and factor do not equal "2", then error If factor is 3 or greater and boats equals 2, then error otherwise pass <?php if($_POST['boats']!="2" || $_POST['factor']!="2"){ echo "wrong number of boats"; } else if($_POST['factor']>="3" && $_POST['boats']=="2"){ echo "wrong number of boats"; } else{ //success } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434164 Share on other sites More sharing options...
teng84 Posted January 9, 2008 Share Posted January 9, 2008 $array = array('1','2','3','4');// valid numbers $arra2 = array('1','2','3','4');// valid numbers if(in_array($_POST['boats'],$array) && in_array($_POST['factor'],$array) ){ //stuff here } else{ //stuff here } or========================this----------------- $array = array('1','2','3','4');// valid numbers if($_POST['boats']=="2"){ if(in_array($_POST['factor'],$array )){ //stuff here } else{ ////invalid } } else{ //invalid } maybe that is not what you exactly need but maybe that might help Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434171 Share on other sites More sharing options...
TheFilmGod Posted January 9, 2008 Share Posted January 9, 2008 <?php $boats = $_POST['boats']; $factor = $_POST['factor']; if ($boats == 2) { if ($factor >= 2) { echo "Correct...."; } else { echo "correct number of boats but wrong factor"; } } else { echo "Incorrect number of boats"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434174 Share on other sites More sharing options...
TimUSA Posted January 9, 2008 Author Share Posted January 9, 2008 Thank you all for your help again...man i hate being a newbie at this. This project is probably taking me 5 times the amount of time it would take you guys...i wasn't even think of arrays: //DISPLAY ERROR MESSAGES $errormsg = "Wrong Number Of Boats For Race Type"; $array = array('1','3','4'); $array2 = array('2'); if ($_POST['boats'] < 2) { $enableform= 'false'; } else if ($_POST['boats'] == 2) { if(in_array($_POST['factor'],$array )){ $enableform = 'false'; } } else if ($_POST['boats'] > 2) { if(in_array($_POST['factor'],$array2 )){ $enableform = 'false'; } } else { $enableform = 'true'; } if ($enableform == 'false') { echo $errormsg; } else { echo "passed"; } Quote Link to comment https://forums.phpfreaks.com/topic/85067-solved-if-and-whats-wrong-here/#findComment-434325 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.