It is currently Wed Feb 08, 2012 5:51 pm

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: SQL from form
PostPosted: Mon Feb 25, 2008 1:34 pm 
Offline

Joined: Sat Feb 23, 2008 9:25 pm
Posts: 1
Hi,
I am a newbie, this is my first post (please go easy).

Iam at the moment trying to set up a query for someone looking for a property on an estate agents website.

From a drop down menu, the user can:
select an area (where they may like to live) from a list of areas.
select an amount of bedrooms from a list of bedrooms
select a minimum price from a list of prices
select a maximum price from a list of prices.

The query I worked out for this is as follows:
$data = mysql_query("SELECT * FROM property WHERE area like '$area' and bedrooms like '$bedrooms' AND price BETWEEN '$min_price' AND '$max_price'") or die(mysql_error());

This seems to work fine and shows all the properties that meet the criteria onto my webpage.

However, I then thought, someone may not care which area they live in and want to see all properties in all the areas, so I decided to add the option 'All areas' to my 'areas' list, I then did the same for the other lists, eg 'all bedrooms' option to my bedrooms list and so on.

I am now trying to write a query that incorporates where the 'all..' option is selected and have become very stuck!
Can someone set me off in the right direction for this.
I hope that makes sense?!?!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 5:13 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6500
Location: toronto, canada
if the value is all, then you want to exclude the element in the where clause (ie you don't want to add it)

Code:
$sql =" select * from table where 1=1 ";

if((string)$_POST['bedrooms'] != "ALL"){
  $sql .= " and bedrooms = " .  (int)$_POST['bedrooms'] ;
}


_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron