Jump to content

Help needed with search script


rvdb86

Recommended Posts

I would really appreciate help with this search function i am trying to make;

 

I have a search form and an array of products from a database to compare against each other.

 

I have made the first step which checks the form data against the array if all the form fields are filled out:

// IF ALL SEARCH FIELDS ENTERED
if ($_POST["apType"] != "0" && $_POST["apArea"] != "0" && $_POST["numGuests"] != "0" && $_POST["numBathrooms"] != "0" && count($_POST["appliance"]) > 0 && count($_POST["amenities"]) > 0 && count($_POST["business"]) > 0){

if ($apartment["TypeId"] == $_POST["apType"] && $apartment["Area"] == $_POST["apArea"] && $apartment["Guests"] == $_POST["numGuests"] && $apartment["Bathrooms"] == $_POST["numBathrooms"]){

	//Check if appliances selected are in the apartment array
	$ApE = 0;
	foreach ($_POST["appliance"] as $ap){
		if(in_array($ap , $apartment["Appliances"])){
			// If they are in the array increase ApE
			$ApE++;
		}
	}

	//Check if amenities selected are in the apartment array
	$AmE = 0;
	foreach ($_POST["amenities"] as $am){
		if(in_array($am , $apartment["Amenities"])){
			// If they are in the array increase AmE
			$AmE++;
		}
	}

	//Check if business selected are in the apartment array
	$BuE = 0;
	foreach ($_POST["business"] as $bu){
		if(in_array($am , $apartment["Business"])){
			// If they are in the array increase BuE
			$BuE++;
		}
	}	

	if ($ApE > 0 && $AmE > 0 && $BuE > 0){

		// IF ALL CRITERIA MATCHES PRINT INFO

		echo $apartment["Address"]."<br />";
	}


}

}

 

The thing is that this only works if the user completes the whole form, but ideally the script needs to check which fields were filled in and do the corresponding checks against the array.

 

Anyone know how I can improve the above script do to this? TIA!

Link to comment
https://forums.phpfreaks.com/topic/181187-help-needed-with-search-script/
Share on other sites

I have made a bit more progress but still need help!

 

I am trying to create a script that checks that the posted values from a form, exist in an array.

 

This is simple if the user has to fill out all the fields, but they don't so I need to check the $_POST array to see which variables exist, and then check each variable in the array of results from the DB.

 

Heres my code so far:

 

$SearchResult = array();
foreach ($apartmentsList as $key1 => $apartment){	


foreach($_POST as $key2 => $p){


	if($_POST[$key2] === $apartment[$key2] && $_POST[$key2] != "advancedSearch"){
		$SearchResult[$apartment["Id"]][$key2] = $_POST[$key2]; 

	}
}
}


$sfields = count($_POST); // FOR TESTING PURPOSE, COUNT HOW MANY POSTED VARIABLES
$results = count($SearchResult); // FOR TESTING PURPOSE, COUNT HOW MANY MATCHES THERE ARE IN ARRAY

echo "<h4>totral sFields = ". $sfields. "</h4>"; // FOR TESTING PURPOSE
	echo "<pre>".print_r($_POST)."</pre>"; // FOR TESTING PURPOSE
echo "<h4>totral Results = ". $results. "</h4>"; // FOR TESTING PURPOSE
	echo "<pre>".print_r($SearchResult)."</pre>"; // FOR TESTING PURPOSE


//****** THIS PART DOESN'T CHECK THAT ALL POSTED VARIBALES MATCH
if (in_array($_POST, $SearchResult)){

// PRINT MATCHES
echo "<hr><h2>Search Results</h2>";
echo "<pre>";
print_r($SearchResult); 
echo "</pre>";
}

 

This code manages creates the array $SearchResult that holds the results where at least one posted variable matches the array. How do I ensure that ALL posted variables are in the array?

 

Thanks in advanced, any suggestions are really welcome

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.