Hi,
I am currently have a basic understanding of PHP and am having trouble with creating a search string for a website im building.
Basically it's a property listings website where the user enters a 'town' or 'postcode' and the database outputs the results.. pretty straight forward however I have added extra search fields such as 'min price' 'max price' 'property type' and 'bedrooms' ... I can display the results for town and postcode but when the user adds one of the other fields or search parameters .. it does not effect the results ..
Here is what I have and i'm using variables to create a database query (not sure if its the best method)The code is not complete and I have not cleaned all the variables etc etc
<?php
//This is only displayed if they have submitted the form
echo "<h2>Results</h2><p>";
$location = $_POST['location'];
$min_rent = $_POST['rent_price_min'];
$max_rent = $_POST['rent_price_max'];
$type = $_POST['property_type'];
$beds = $_POST['beds'];
//Error
if ($location == "")
{
echo "<p>Please enter a Place or Postcode";
exit;
}
// Connect to our Database
mysql_connect("127.0.0.1", "root", "") or die(mysql_error());
mysql_select_db("rentals") or die(mysql_error());
// Filtering
$location = strtoupper($location);
$location = strip_tags($location);
$location = trim ($location);
// Build dyanamic search query variables
if ($min_rent == ''){
$min = "";
}else{
$min = "AND property_rent >= '$min_rent' ";
}
if ($max_rent == ''){
$max = "";
}else{
$max = "AND property_rent <= '$max_rent' ";
}
if ($type == ''){
$ty = "";
}else{
$ty = "AND property_type = '$type' ";
}
if ($beds == ''){
$bd = "";
}else{
$bd = "AND property_beds = '$beds' ";
}
//Search database query
$data = mysql_query("SELECT * FROM submissions WHERE
property_town LIKE '%$location%' OR property_postcode LIKE '%$location%' OR property_county LIKE '%$location%'
".$min." ".$max." ".$ty." ".$bd."") or die(mysql_error());
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
echo "'$anymatches' Properties found !";
echo "<br>";
//And we remind them what they searched for
echo "<b>You Searched For:</b> " .$location;
echo "<br>";
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo "Location: " .$result['property_town'];
echo "<br>";
echo "Monthly Rent: £" .$result['property_rent'];
echo "<br>";
echo "Property Type: " .$result['property_type'];
echo "<br>";
echo "Bedrooms: ".$result['property_beds'];
echo "<br>";
echo "Property Description: ".$result['property_description'];
echo "<br>";
}
?>
There is alot probably wrong with this but if I can get search working I can take it from there .. Would be grateful for any tips or pointers ?