Link to home
Start Free TrialLog in
Avatar of Benson_nc
Benson_nc

asked on

Building sql select statement for results page using PHP

I'm trying to concatenate a sql query string using PHP but not having much luck. I'm processing a form that has dropdown menus for minprice and maxprice (these will always have a value), minsqft and maxsqft dropdown menus (these will also always have a value). The PropertySubtype and Status are OK for this case.

I'm having the problem with the "optional" query terms: beds, waterview, and golf.  Beds is a dropdown menu with integer values BUT I would also like to make this field have some kind of a null value so that it won't affect the query results. Right now I have an empty value passed if the visitor picks "Select All."

For waterview, I'm looking for 'Yes' in the dB and the form's checkbox checked value is 'Yes'

For golf, the dB field contains comma separated values but the value I'm looking for is 'Golf Course Frontage' which is the checked value of the checkbox.
CASE "home":
$query = "SELECT * FROM Listings WHERE ListingPrice BETWEEN $minprice AND $maxprice AND SquareFootage BETWEEN $minsqft AND $maxsqft AND PropertySubtype1 = 'Site Bui' AND Status = 'Active' . "'";
 
if (!empty($beds))
{$query .= " AND Bedrooms = '" . $beds . "'";}
 
if (isset($waterview))
{$query .= " AND WATERVIEW LIKE '" . $waterview . "'";}
 
if (isset($golf))
{$query .= " AND PROPERTIES LIKE '" . $golf";}
 
break;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of StraySod
StraySod
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Benson_nc
Benson_nc

ASKER

Thanks for the correction. All is working now.  The only addition I had to make was to put %% around $golf since there was additional text in the dB field different from what I was looking for.