Jump to content

php form data help


dassa

Recommended Posts

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 ;D ;D

 

cheers

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/85154-php-form-data-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/85154-php-form-data-help/#findComment-434449
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.