0

I am trying to compare IF statements.

if ($_POST['price'] =="see all"){
//show okolo 
} 
else if ($_POST["accommodation"] =="see all"){
//show chibuzo 
} 

This works but as soon as I add:

else if ($_POST['price'] =="see all" AND $_POST['accommodation"]=="see all"){ 
//show henry 
} 

This stuff does not work, it now shows "okolo" instead of "henry

Edited by uchejava

5
Contributors
8
Replies
32
Views
2 Years
Discussion Span
Last Post by uchejava
0

That's right as the first if condition is satisfield.

Try putting the last one at the start instead.

if ($_POST['price'] =="see all" AND $_POST['accommodation"]=="see all")
{ 
    //show henry    
}
else if ($_POST['price'] =="see all")
{
    //show okolo 
} 
else if ($_POST["accommodation"] =="see all")
{
    //show chibuzo 
} 

Edited by DaveAmour

0

In addition, there is a small typo with quotes:

$_POST['accommodation"]

Use single quotes or double quotes, not both together:

$_POST['accommodation'] # or
$_POST["accommodation"]

Bye!

0

I am still having the same error

if ($_POST['accommodation'] == "see all" AND $_POST['price']=="see all"){

}        

else if ($_POST['accommodation']=="see all"){

}

else if ($_POST['price']=="see all"){

}
0

Works fine for me, try to trim the input, if you get a space at the beginning or at the end of the string then the comparison will fail, so before the statements do:

$_POST = array_map('trim', $_POST);

Docs: http://php.net/array-map

0
if ($_POST['accommodation'] == "see all" && $_POST['price']=="see all"){

}

Use && instead of and because it is php. 
0

Just a tip. If you are collecting this data from select dropdowns, then it's usually easier to set the 'see all' value to '', that way you can do something like:

if(!$_POST['accommodation'] && !$_POST['price'])

From the select:

<select name="accommodation">
    <option value="">All<option>
    ...other options prob from DB...
</select>
0

Also @Nilesh_4

Hi! In PHP you can use both. But there's to say that && has an higher precedence than AND, which defines how the conditions are grouped. So for example:

<?php

    $a = true;
    $b = true;
    $c = false;

    $d = $a === true && $b === true AND $c === true;
    echo var_dump($d);

    $d = $a === true && $b === true && $c === true; 
    echo var_dump($d);

Outputs:

test #1
bool(true)

test #2
bool(false)

If the conditions are set into an IF statement, instead of being assigned to a variable, the expression returns false in both cases.

More information here:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.