Jump to content

[SOLVED] if and whats wrong here?


TimUSA

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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 

Link to comment
Share on other sites


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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.