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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dgny06
Forum Commoner
Posts: 25
Joined: Thu Jun 14, 2007 11:35 pm

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

Post 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);
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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().
dgny06
Forum Commoner
Posts: 25
Joined: Thu Jun 14, 2007 11:35 pm

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

Post by dgny06 »

that was it.....thanks!!!
Post Reply