Jump to content

How to eliminate drop down selections from SQL query


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());

?>

 

 

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.