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

Recommended Answers

All 8 Replies

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 
} 

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!

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"){

}

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

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

}

Use && instead of and because it is php. 
Member Avatar for diafol

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>

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:

Thank you guys. I have fixed it with your suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.