dassa Posted January 9, 2008 Share Posted January 9, 2008 hi all hope somebody can help as i can not find topics covering this problem. i have built a form with various options $location , $accommodationType, $numberOfBedrooms, i want users to be able to search by either all fields or just one or even none just search all result types. the problem i am iencountering is checking to see which fields have been entered and which ones to use to building the mysql query. any help will be useful ;D ;D cheers Quote Link to comment https://forums.phpfreaks.com/topic/85154-php-form-data-help/ Share on other sites More sharing options...
adam291086 Posted January 9, 2008 Share Posted January 9, 2008 look here http://uk2.php.net/manual/en/function.empty.php. This fuction determins whether a variable is empty. If empty don't use on database. Quote Link to comment https://forums.phpfreaks.com/topic/85154-php-form-data-help/#findComment-434448 Share on other sites More sharing options...
kratsg Posted January 9, 2008 Share Posted January 9, 2008 Well, first, can you explain what html form element is being used for this purpose of multiple-searching? You would be dealing with an array of at least one, and at most 3 items. (Of course, you can always take all 3 values, check them, add them into an array, and loop through to make a sql statement). <?php $search_fields = array("Location"=>$location,"Accomodation"=>$accomodationType,"Bedrooms"=>$numberOfBedrooms); $where_clause = null; foreach($search_fields as $field=>$value){ if($value != '' || isset($value) || !empty($value)){//it is not empty $value = mysql_real_escape_string($value); //after checking the values, build the query if($where_clause == null){$where_clause .= "`$field` = '$value' ";} else {$where_clause .= "AND `$field` = '$value' ";} }//end the validation condition }//end the foreach $query = "SELECT * FROM `table` WHERE $where_clause LIMIT 10 SORT BY `field` ASC"; ?> That should give you a nudge in the right direction. The where_clause is what contains the array of the values you will search for. Quote Link to comment https://forums.phpfreaks.com/topic/85154-php-form-data-help/#findComment-434449 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.