Jump to content

How to eliminate drop down selections from SQL query


AdRock

Recommended Posts

I have a problem with some SQL queries so here is what i'me trying to do

 

This is part of my form where I select a number of bedrooms

<select name="minBeds" id="minBeds" style="width:152px">
   <option value="Any">Any</option>
   <optgroup label="----"></optgroup>
   <option value="0">Studio</option>
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5 or more</option>
</select>

 

and here is my query but it only works if I have selected a number and not 'Any'

mysql_query("SELECT COUNT(*) FROM search WHERE (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type' AND `keywords` LIKE '%$keywords%') OR
(price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type')")

 

How do I do it so if a user selects any, it eliminates that from the query?  Would I have to create some IF statements to check if it has been selected and create a new query?

Give This a try.

 

<?php

$query = "SELECT COUNT(*) FROM search WHERE (price BETWEEN $min_price AND $max_price":

if (!empty($minBeds))
   $query .= " AND bedrooms >= $min_bedrooms";

$query .= " AND location = '$location' AND type = '$property_type' AND `keywords` LIKE '%$keywords%') OR (price BETWEEN $min_price AND $max_price";

if (!empty($minBeds)){
$query .= " AND bedrooms >= $min_bedrooms";

$query .= " AND location = '$location' AND type = '$property_type')";

$result = mysql_query($query)or die(mysql_error());

?>

 

 

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.