Jump to content

*solved* variable comparison/logical operator problem


Switch0r

Recommended Posts

hey peeps, i wonder if anyone can help me with my predicament...im sure someone can, cos it should be fairly simple but the answer keeps evading me...

here goes.

i have some variables sent from a form, 10 in total, and i want to make sure that they meet a required level. i have var 1, and var a > i. I want either var 1 to equal 1 & a > i to be 0, or var 1 to be 0 and at least one of a > i to be 1, but not both at the same time...if that makes any sense at all

any thoughts? this is driving me crazy :)
That sounded a bit confusing, but I guess you may be looking for the XOR operator.

[a href=\"http://www.php.net/manual/en/language.operators.logical.php\" target=\"_blank\"]http://www.php.net/manual/en/language.operators.logical.php[/a]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Xor - TRUE if either $a or $b is TRUE, but not both [/quote]
well thats what i thought, and i'd been trying various methods of doing that

what i had so far is this: id make a check if var a > i is set to 1, and if so, update a check variable, to make sure at least one of them is set, then compare this check var with the value of var 1

[code]if($var 1 < 1 xor $check_var < 1){update a second check variable}[/code]

but either way it comes up with the same answer, with no vars selected, 1 or the other, or all selected...
[!--quoteo(post=383120:date=Jun 13 2006, 06:33 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 06:33 AM) [snapback]383120[/snapback][/div][div class=\'quotemain\'][!--quotec--]
well thats what i thought, and i'd been trying various methods of doing that

what i had so far is this: id make a check if var a > i is set to 1, and if so, update a check variable, to make sure at least one of them is set, then compare this check var with the value of var 1

[code]if($var 1 < 1 xor $check_var < 1){update a second check variable}[/code]

but either way it comes up with the same answer, with no vars selected, 1 or the other, or all selected...
[/quote]

what if you add =<
[code]
if($var 1 =< 1 xor $check_var =< 1){update a second check variable}
[/code]
well ive changed the plan slightly...hopefully less complicated now :)

i now have 9 checkboxes on the form, and at least one of them needs to be checked to proceed.

this is what ive got so far:
[code]
// setup
$count = 0;
if($var1 = 1){$count++;}
if($var1 = 2){$count++;}
if($var1 = 3){$count++;}
if($var1 = 4){$count++;}
if($var1 = 5){$count++;}
if($var1 = 6){$count++;}
if($var1 = 7){$count++;}
if($var1 = 8){$count++;}
if($var1 = 9){$count++;}

// check
if($count = 0){Give error}
[/code]

only this still doesnt work
[!--quoteo(post=383132:date=Jun 13 2006, 08:19 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 08:19 AM) [snapback]383132[/snapback][/div][div class=\'quotemain\'][!--quotec--]
well ive changed the plan slightly...hopefully less complicated now :)

i now have 9 checkboxes on the form, and at least one of them needs to be checked to proceed.

this is what ive got so far:
[code]
// setup
$count = 0;
if($var1 = 1){$count++;}
if($var1 = 2){$count++;}
if($var1 = 3){$count++;}
if($var1 = 4){$count++;}
if($var1 = 5){$count++;}
if($var1 = 6){$count++;}
if($var1 = 7){$count++;}
if($var1 = 8){$count++;}
if($var1 = 9){$count++;}

// check
if($count = 0){Give error}
[/code]

only this still doesnt work
[/quote]

Your if statement is wrong here you need if($var1 == 9){$count++;} [i]note the 2 ==[/i] otherwise your applying a new value to $var1.
You have to use == as equal or you are setting the vars as new varable names.

was betten dam lol

[code]
// setup
$count = 0;
if($var1 == 1){$count++;}
if($var1 == 2){$count++;}
if($var1 == 3){$count++;}
if($var1 == 4){$count++;}
if($var1 == 5){$count++;}
if($var1 == 6){$count++;}
if($var1 == 7){$count++;}
if($var1 ==8){$count++;}
if($var1 ==9){$count++;}

// check
if($count == 0){Give error}

[/code]
[!--quoteo(post=383135:date=Jun 13 2006, 08:28 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 08:28 AM) [snapback]383135[/snapback][/div][div class=\'quotemain\'][!--quotec--]
well ive got them all with == instead, and no matter how many boxes i choose (other than 0), the $count remains at 0, muh?
[/quote]

ok,

as a test you could put a echo var1 to make sure that the variable is being passed.

think you might need something like.

[code]
$var1 = $_POST['var1'];
[/code]

before your if statement.
[!--quoteo(post=383135:date=Jun 13 2006, 07:28 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 07:28 AM) [snapback]383135[/snapback][/div][div class=\'quotemain\'][!--quotec--]
well ive got them all with == instead, and no matter how many boxes i choose (other than 0), the $count remains at 0, muh?
[/quote]

Try like this for a moment what happens.

[code]
<?

echo $var1;

if($_POST('$submit') {

$count = 0;
if($var1 == 1){$count++;}
if($var1 == 2){$count++;}
if($var1 == 3){$count++;}
if($var1 == 4){$count++;}
if($var1 == 5){$count++;}
if($var1 == 6){$count++;}
if($var1 == 7){$count++;}
if($var1 ==8){$count++;}
if($var1 ==9){$count++;}

if($var1 ==0){echo " Sorry i am not working!";}
}
?>
[/code]
This should grab the posted variable and place it in a local variable.

to make sure the information has been passed you can delete the comment tags and the variable will be printed to the screen.

[code]
// setup

$var1 = $_POST['var1'];

// echo "$var1";

$count = 0;

if($var1 == 1){$count++;}
if($var1 == 2){$count++;}
if($var1 == 3){$count++;}
if($var1 == 4){$count++;}
if($var1 == 5){$count++;}
if($var1 == 6){$count++;}
if($var1 == 7){$count++;}
if($var1 == 8){$count++;}
if($var1 == 9){$count++;}

// check
if($count == 0){Give error}
[/code]
right, well...ive sorted it :)

added the $_POST array vars for the comparison, but changed the check so it checks if its "on" or not, as i had previously thought that checkboxes gave either 1 (checked) or 0 (blank), which apparently is untrue as it comes up as: on (checked) or empty var (blank)

thanks for the help tho peeps [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.