Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searches

Status
Not open for further replies.

manicleek

Technical User
Joined
Jun 16, 2004
Messages
143
Location
GB
I am trying to make a property site where users search by location, number of rooms and/or price

however I only want the fields to appear in the SQL if they have been used, so for example if they only enter something in the location field the sql would be

Code:
SELECT * FROM villadesc WHERE location='$location'

if the enter 2 fields, say rooms

Code:
SELECT * FROM villadesc WHERE location='$location' AND rooms='$rooms'
and on

I am using GET to pass the query and I am guessing I need to do some kind of string to make this work but have no idea how
 
u have to use if statements:
Code:
$sql="SELECT * FROM villadesc WHERE ";
$AndUsed=0;
if($location!="")
{
 $sql.=" location like '%$location%'";
 $AndUsed=1;
}

if($rooms!="")
{
 if($AndUsed==0)
  $sql.=" rooms like '%$rooms%'";
 else
  $sql.=" and rooms like '%4rooms%'";
 $AndUsed=1;
}
so on and so forth...

Known is handfull, Unknown is worldfull
 
Thanks for that

How does that know to enter the text onto the end of the $sql is it all to do with the . after the $sql in the if statements?
 
>>How does that know to enter the text onto the end of the $sql is it all to do with the . after the $sql in the if statements?


i dont get it...

Known is handfull, Unknown is worldfull
 
What I mean is, the origional value for $sql is

Code:
$sql="SELECT * FROM villadesc WHERE ";

then there is the if statements which contain the end of the sql statement depending on what was searched by.

how does it know to put these on the end of the origional sql statement after WHERE.

I noticed that you put a . after $sql in the if statements
i.e.

$sql.="rooms etc

does that have something to do with it as I thought if there was a ; at the end of the initial sql query that would make server think that was it.
 
yes the . appends the value to the string...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top