Have trouble in a if condition

The if below is working ok, it check when indexes, name, zipcode and state are empty.

if ( trim($_POST['name']) == '' && $_POST['zipcode'] =='' && $_POST['state'] =='' 

But when it comes to check the array types I don’t know how to structure in a way that it checks weather is empty or not. I did it as below and it fails the test. How can I insert the types index array and check when is empty? empty meaning when there is not value remembering that it is an array now.

if ( trim($_POST['name']) == '' && $_POST['zipcode'] =='' && $_POST['state'] =='' && isset($_POST['frmSearch']['types']) ) 
{ this} 
else 
{that}

If the array will ALWAYS exist you can just use if($array) which will evaluate true if there’s anything in it, and false if it’s an empty array (or zero, null, false, empty string).

If the array may or may not exist you’ll need if(isset($_POST[‘frmSearch’][‘types’]) AND !$_POST[‘frmSearch’][‘types’]) { … }

Thank you for clearing me that. Now that being the case I have compare the array to empty string like

if ( $_POST['frmSearch']['types'] == '' )

to false

if ( $_POST['frmSearch']['types'] == false )

To Null

if ( $_POST['frmSearch']['types'] == NULL )

to zero

if ( $_POST['frmSearch']['types'] == 0 )

And it works ok, becasue it is empty there fore it will display a message for instance "no results were found.

But when it evalutes to true meaning there is something in it, it will still display “no Results were found” as it was emtpy.

I make a print_r or a var_dump and when assign something to the array, it shows is not empty, there is something in it, which means it evaluates to true at that points. the var dump and print_r looks as fallows when the array has something in it, or evaluates to true meaning is not empty.

var_dump

  array(5) {

[“name”]=>
string(0) “”
[“zipcode”]=>
string(0) “”
[“state”]=>
string(0) “”
[“frmSearch”]=>
array(1) {
[“types”]=>
array(1) {
[0]=>
string(1) “5”
}
}
[“submit”]=>
string(6) “Submit”
}

print_r

Array

(
[name] =>
[zipcode] =>
[state] =>
[frmSearch] => Array
(
[types] => Array
(
[0] => 5
)

    )

[submit] => Submit

)

The var_dump and print_r clearly shows that the array is not empty and there is something in it. But still it will display the message “no results were found” when the test is as below.

if ( trim($_POST['name']) == '' && $_POST['zipcode'] =='' && $_POST['state'] =='' && $_POST['frmSearch']['types']==false ) 

{ echo"no results were found"} 

else 

{valid code...}

I thought that the test that the types array is submitted to should echo no results when it evaluates to false, empty, 0, or NULL, but if it evaluates to true it should go to the valid code. But it still displaying the “no results were found” when it evaluates to true or false.

I still don’t understand, is there something wrong with the test? The test will work as expected when I take the array types test

&& $_POST['frmSearch']['types']==false

I think the code is working properly, but your issue is something else.

My guess- You’re probably modifying the _POST array in your code. var_dump() it on the line before your if statement so you can see the values those variable hold at that specific point in your script.

hit submit without putting any input with if then statement like

if ( trim($_POST['name']) == '' && $_POST['zipcode'] =='' && $_POST['state'] =='' && $_POST['frmSearch']['types']==false ) 

the resulting var_dump is as below:

array(5) {

[“name”]=>
string(0) “”
[“zipcode”]=>
string(0) “”
[“state”]=>
string(0) “”
[“frmSearch”]=>
array(1) {
[“types”]=>
array(1) {
[0]=>
string(0) “”
}
}
[“submit”]=>
string(6) “Submit”
}

And the result of that expression in the if statement is false. This will not cause your “no results” message to print(at least not the way you represented the logic in the broken code). If it does, your issue is elsewhere. Maybe post some real code, but only after you isolate and test it by putting it in its own file.

Btw- while not related to your immediate issue, an array is only “falsy” if it is really empty. An array that has an empty string in it, isn’t really empty. count($array) needs to be 0. This would possibly be your next problem :slight_smile: http://www.php.net/manual/en/language.types.boolean.php

I like to use the terms truthy and falsy, for values that convert to boolean true, or false.

Just to make sure this means that the user hasn’t put anything in the array field right?

&& $_POST['frmSearch']['types']==false ) 

if ( trim($_POST['name']) == '' && $_POST['zipcode'] =='' && $_POST['state'] =='' && isset($_POST['frmSearch']['food_types'])?$_POST['frmSearch']['types']:array()) 

{

 echo '<div id="tresuno">';
 
 echo'<p class="tremendi">No results in your match please try again!</p>

<div id="tresdo">
<a href="index3.php"><< Go Back</a>

</div>';} else {

var_dump($_POST);
    $strName = isset($_POST['name'])?mysql_real_escape_string($_POST['name']):'';
    $strZipCode = isset($_POST['zipcode'])?mysql_real_escape_string($_POST['zipcode']):'';
    $strState = isset($_POST['state'])?mysql_real_escape_string($_POST['state']):'';
    $arrFoodTypes = isset($_POST['frmSearch']['food_types'])?$_POST['frmSearch']['types']:array();
    $arrOfferings = isset($_POST['frmSearch']['offerings'])?$_POST['frmSearch']['offerings']:array();

some html and php code.

I remember you say something about repeating the $_POST variable. In the else statement the POST variable is used as well and I don’t know if it is conflicting some how with the one used in the if statement. They are both used differently. could that be conflicting?

Are you aware that your if statement has different code in it that what you’ve been representing so far? No wonder it’s behavior is off.

Sorry, I’m going to sleep.

What I really meant was this

&& $_POST['frmSearch']['types']==false )

instead of this

isset($_POST['frmSearch']['food_types'])?$_POST['frmSearch']['types']:array()) 

really don’t even have any idea where this problem is coming from…

Thank you, tomorrow man.

If the frmSearch field is a text box it will always be present in POST (even if the user left it empty), and so $_POST[‘frmSearch’][‘types’] will always have at least 1 value (and therefore never be an empty array).

Maybe if $_POST[‘frmSearch’][‘types’] != ‘’ is what you want? Depends on your form.

Have to say that the form field is a Select field, I was looking for how select fields passes weather the field was left empty or not by the user, but didn’t find any information in it. I talked about a input text field what about a select field? how does it passes when is left empty or put a value in it?

Well this is how it looks

  <li class="restaurants-food-types">
                        <label for="restaurants-food-types">Food Type</label>
                        <select name="frmSearch[types][]" id="restaurants-food-types">
                            <?php
                            foreach($arrRestaurantsFoodTypes['values'] as $intIndex=>$intFoodTypesId) {
                                printf(
                                    '<option value="%s"%s>%s</option>'
                                    ,$intFoodTypesId
                                    ,in_array($intFoodTypesId,$arrFoodTypes)?' selected="selected"':''
                                    ,$arrRestaurantsFoodTypes['output'][$intIndex]
                                );
                            }    
                            ?>
                        </select>
                    </li>

what does this part of the script suggest? and how will it pass when left in blank or put a value in it? and how it can be test it in a if statement like we have been trying to do when it’s an empty field {echo"no results for your match"} else {valid code}.

Thank you.

$_POST[‘frmSearch’][‘types’][0] == ‘’

You need to check one of the elements in the array. The first element should always be there with that form.

Crmalibu why putting the index there was needed to be put?

I put exactly how you told me and now it works. I believe since this is a array index type then it need to be differentiated from the others ones.

But that’s should not be the exact answer…

thank you Crmaibu.

Because an array only converts to boolean false if it has zero elements. Your array had 1 element.

To compare the array to a boolean value, php must convert it to boolean using it’s defined rules. By specifying an index, you’re comparing a string to a boolean, which uses different rules. See the previous link I gave.

Yes it makes sense since an array is multiples values while a string is one and can be only false and true. So as you said specifying the indexes are the way to convert an array to a single value Booleans false or true.

thank you Crmalibu

have to bring this topic again because the validation in this form will only validate if all the fields are filled. I want that if only one is true then print as well.

if ( trim($_POST['name']) == '' && $_POST['zipcode'] =='' && $_POST['state'] =='' && $_POST['frmSearch']['types'][0]=='' && $_POST['frmSearch']['offerings'][0]=='') 
{

 echo '<div id="tresuno">';
 var_dump($_POST);
 echo'<p class="tremendi">No results for your match please try again!</p>

<div id="tresdo">
<a href="index.php"><< Go Back</a>

</div>';} else {
//extract the variables from the url and dsiplay the list of restaurants

}

the script above will validate if only all the fields are filled. But I want that if one of the fields are filled and it is it match then print a list of restaurants that matches or at least has a similar match of that of the user input.

I was thinking that the operator && is forcing the user to fill all the fields?

would the operator & be effective for this case?

show us the output of var_dump($_POST);

You would be better off with the OR operator which is ||

Not really. It’s probably correct as is.

This is the vardump() when form is submited empty.

array(1) {
[“frmSearch”]=>
array(5) {
[“name”]=>
string(0) “”
[“zipcode”]=>
string(0) “”
[“state”]=>
string(0) “”
[“types”]=>
array(1) {
[0]=>
string(0) “”
}
[“submit”]=>
string(6) “Submit”
}
}

This is the vardump() when the first input field “name” is filled in

array(1) {
[“frmSearch”]=>
array(5) {
[“name”]=>
string(22) “Caridad at kingsbridge”
[“zipcode”]=>
string(0) “”
[“state”]=>
string(0) “”
[“types”]=>
array(1) {
[0]=>
string(0) “”
}
[“submit”]=>
string(6) “Submit”
}
}

And it is said in the post above when all the fields are filled then there is not vardump(); it will go directly into the else statement.