Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.