Page 1 of 1

Help With Embedded Query...Need To Use Case Clause???

Posted: Sun Jun 22, 2008 11:58 am
by dgny06
As always, thanks in advance for your help.....


I am working on a real estate project and am working on a form that allows the user to select a borough and/or a listing type. The problem is that I am not sure how to work the "or" part of the query if the user only selects one filter from the form (my current query only works if both filters are selected). I know that you probably have to use a "Case" clause but I am not sure how to use it. My code that handles the form variables and inserts them into a query on the PHP page is below: (lines 7 and 8 contain the form variables)

Code: Select all

//Declare variables being passed into this page
$QueryType = $_GET['Type'];
$QueryBorough = $_GET['Borough'];

Code: Select all

$ListingsQuery = "select l.ListingID as ListID, l.Image, l.tbType_TypeID, t.TypeName, b.Borough, l.City, l.Price, l.Rooms, l.OpenHouse
from tbListing l
join tbBorough b on b.BoroughID = l.tbBorough_BoroughID
join tbType t on t.TypeID = l.tbType_TypeID
where l.Void = '0'
and l.Sold = '0'
and l.tbType_TypeID = '$QueryType'
and l.tbBorough_BoroughID = '$QueryBorough'";
$FindListings = mysql_query($ListingsQuery);

Re: Help With Embedded Query...Need To Use Case Clause???

Posted: Sun Jun 22, 2008 12:28 pm
by jayshields
I don't really know what you're getting at, my guess is that you are trying to dynamically create the query?

If so, use something like this:

Code: Select all

$ListingsQuery = "select l.ListingID as ListID, l.Image, l.tbType_TypeID, t.TypeName, b.Borough, l.City, l.Price, l.Rooms, l.OpenHouse
from tbListing l
join tbBorough b on b.BoroughID = l.tbBorough_BoroughID
join tbType t on t.TypeID = l.tbType_TypeID
where l.Void = '0'
and l.Sold = '0'";
 
if(isset($QueryType) && !empty($QueryType)) $ListingsQuery .= " and l.tbType_TypeID = '$QueryType'";
if(isset($QueryBorough) && !empty($QueryBorough)) $ListingsQuery .= " and l.tbBorough_BoroughID = '$QueryBorough'";
If the variables to be used are coming straight from a form then I would also suggest using mysql_real_escape_string().

Re: Help With Embedded Query...Need To Use Case Clause???

Posted: Sun Jun 22, 2008 3:12 pm
by dgny06
that was it.....thanks!!!