co.ador Posted January 9, 2010 Share Posted January 9, 2010 The if below is working ok, it check when indexes, name, zipcode and state are empty. <?php 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} ?> Link to comment https://forums.phpfreaks.com/topic/187813-have-trouble-in-a-if-condition/ Share on other sites More sharing options...
tomdchi Posted January 9, 2010 Share Posted January 9, 2010 Try this <?php if (!empty($_POST['name']) && !empty($_POST['zipcode']) && !emtpy($_POST['state']) && isset($_POST['frmSearch']['types'])) { // all POST data is not empty } else { //someting is empty throw error } ?> when testing for isset or empty you don't need to trim at that point. it won't make any difference in the testing of the variable.[/code] Link to comment https://forums.phpfreaks.com/topic/187813-have-trouble-in-a-if-condition/#findComment-991607 Share on other sites More sharing options...
co.ador Posted January 9, 2010 Author Share Posted January 9, 2010 It won't work still this is very strange Taking the immediate quote below into account below and false if it's an empty array (or zero, null, false, empty string). Thank you for clearing me that. Now that being the case I have compare the array to empty string like <?php if ( $_POST['frmSearch']['types'] == '' ) ?> to false <?php if ( $_POST['frmSearch']['types'] == false )?> To Null <?php if ( $_POST['frmSearch']['types'] == NULL )?> to zero <?php 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. <?php 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 <?php && $_POST['frmSearch']['types']==false?> Link to comment https://forums.phpfreaks.com/topic/187813-have-trouble-in-a-if-condition/#findComment-991700 Share on other sites More sharing options...
JAY6390 Posted January 9, 2010 Share Posted January 9, 2010 function check_empty($val) { if(is_array($val)) { foreach($val as $k => $v) { $ret = check_empty($val); if($ret) return true; } }else{ $val = trim($val); return empty($val) ? true : false; } return false; } This function will tell you if any value in an array is empty (returns true if any value is empty) Link to comment https://forums.phpfreaks.com/topic/187813-have-trouble-in-a-if-condition/#findComment-991706 Share on other sites More sharing options...
co.ador Posted January 9, 2010 Author Share Posted January 9, 2010 right now the function is working if I take the array out... But then when I assign the function to the array now it will take a while for the localhost to connect and then fail this is the set up <?php if ( check_empty($_POST['name']) && check_empty($_POST['zipcode']) && check_empty($_POST['state']) && check_empty($_POST['frmSearch']['types'])) ?> Now at least is failing the message displayed by the browser is Connection Interrupted The connection to the server was reset while the page was loading. The network link was interrupted while negotiating a connection. Please try again. there should be something wrong here... the array POST is causing the connection to be interrupted and it fails only if I put the array inside the if (as a condition) Link to comment https://forums.phpfreaks.com/topic/187813-have-trouble-in-a-if-condition/#findComment-991722 Share on other sites More sharing options...
co.ador Posted January 9, 2010 Author Share Posted January 9, 2010 in google chrome is display this as well to enhance the error Error 101 (net::ERR_CONNECTION_RESET): Unknown error. Link to comment https://forums.phpfreaks.com/topic/187813-have-trouble-in-a-if-condition/#findComment-991726 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.