Forum Moderators: open
Here is what I need to do:
I have a search form and the fields are for multiple mysql tables. Many of these fields are dropdowns that are populating dynamically from the db. I need to write a where clause to allow the visitor to choose "all" in one or more dropdowns.
I can write the query for multiple tables but I am stuck on how to write a where clause that will both allow searching "all" and also for multiple tables.
What I have so far will let me do the "all" searching but I don't know how to build the query to use it on mutiple tables. Here is what I wrote for a where clause:
$whereclause;
while (list($key, $value) = each ($HTTP_POST_VARS)) {
if (($value != "all") && ($key != "Submit") && ($key != "results")) {
$whereclause .= $key . "='" . $value . "' AND ";
}
}
if ($whereclause != "") {
$whereclause = substr($whereclause, 0, -4);
}
$whereclause = "WHERE " . $whereclause;
And here is my query:
$result = mysql_query("SELECT r.ResortName, r.City, r.State, r.Country, l.UsageType, l.ListingType, l.Bedrooms, l.Bathrooms FROM tblResort r, tblListing l $whereclause") or die(mysql_error());
I need to have all of the above fields be searchable by choosing a value in the dropdown OR selecting "All" in any of them.
Any help would be greatly appreciated. I have been struggling with this for two days!
Here is my 95% working code:
$whereclause;
while (list($key, $value) = each ($HTTP_POST_VARS)) {
if (($value != "all") && ($key != "submit") && ($key != "results") && ($key != "searchtype")) {
$whereclause .= $key . "='" . $value . "' AND ";
}
}
if ($whereclause != "") {
$whereclause = substr($whereclause, 0, -4);
}
$whereclause = "WHERE tblResort." . $whereclause . " AND tblListing." . $whereclause;
$result = mysql_query("SELECT tblResort.ResortID, tblResort.Country, tblResort.State, tblResort.City, tblListing.Bedrooms, tblListing.Bathrooms, tblListing.UsageType, tblListing.ListingType FROM tblResort JOIN tblListing ON tblResort.ResortID=tblListing.ResortID $whereclause") or die(mysql_error());
$HTTP_POST_VARS
This has been deprecated in PHP, you really should be using the latest $_POST superglobal array now.
One of the easiest ways to figure out where your query is failing is to dump the statement to the browser prior to executing it (or better yet, dump the statement to the browser upon failure of executing it, but only if you are running in a test environment, not LIVE)
<form action="search.php" method="post">
<input type="hidden" name="searchtype" value="Advanced">
/////
<select name="ResortID" style="width:200px;">
<option value="all">All Resorts</option>
<option value="815">Alhambra @ Poinciana</option>
<option value="748">Allegro Resort</option>
<option value="623">Ameila Resort</option>
<option value="200">American Resorts International</option>
<option value="365">Americano Resort</option>
<option value="47">Apollo Park</option>
/////
<select name="Country" style="width: 200px;">
<option value="all">All Countries</option>
<option value="United States">United States</option>
<option value="">----------------------</option>
<option value="Antigua">Antigua</option>
<option value="Aruba">Aruba</option>
<option value="Austria">Austria</option>
<option value="Bahamas">Bahamas</option>
<option value="Barbados">Barbados</option>
/////
<select name="State" style="width: 200px;">
<option value="all">All States</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
<option value="California">California</option>
<option value="Colorado">Colorado</option>
/////
<select name="City" style="width: 200px;">
<option value="all">All Cities</option>
<option value="Acapulco">Acapulco</option>
<option value="Alberta">Alberta</option>
<option value="Alexandria">Alexandria</option>
<option value="Altamonte Springs">Altamonte Springs</option>
/////
<select name="Bedrooms" style="width: 75px;">
<option value="all">All</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
/////
<select name="Bathrooms" style="width: 75px;">
<option value="all">All</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
/////
<select name="UsageType" style="width: 75px;">
<option value="all">All</option>
<option value="Annual">Annual</option>
<option value="Bi-Annual">Bi-Annual</option>
/////
<select name="ListingType" style="width: 75px;">
<option value="all">All</option>
<option value="Rental">Rental</option>
<option value="Sale">Sale</option>
<option value="Rental/Sale">Rental/Sale</option>
WHERE tblResort.ResortID=ALL
This will obviously throw an error since "ALL" is not an integer (I assume that tblResort.ResortID is an INT field).
So you need to change your logic in PHP when you build your SQL statement.
For example, if "ALL" is selected, then don't include that constraint in your WHERE clause.
One last bit- if all of your selected options are "ALL" then you may end up with an incorrect WHERE statement, which will also throw an error. So you either have to not include a WHERE clause in that case, or you can include a catchall (like "1=1").
Make sense? Any fresh ideas of how I can search multiple tables, with "All" as a choice in each dropdown?
[edited by: LifeinAsia at 4:25 pm (utc) on Oct. 22, 2008]
$mysqlSelect = "SELECT tblListing.ListingID, tblListing.ResortID, tblListing.Bedrooms, tblListing.Bathrooms, tblListing.isFloat, tblListing.WeeksAvail, tblListing.UsageType, tblListing.ListingType, tblResort.City, tblResort.State FROM tblListing LEFT JOIN tblResort ON tblListing.ResortID=tblResort.ResortID ";
$mysqlSelect .= "WHERE 1=1";
if($ResortID) $mysqlSelect .= "tblResort.ResortID='" . $ResortID . "'";
if($Country) $mysqlSelect .= " AND tblResort.Country='" . $Country . "'";
if($State) $mysqlSelect .= " AND tblResort.State='" . $State . "'";
if($City) $mysqlSelect .= " AND tblResort.City='" . $City . "'";
if($Bedrooms) $mysqlSelect .= " AND tblListing.Bedrooms='" . $Bedrooms . "'";
if($Bathrooms) $mysqlSelect .= " AND tblListing.Bathrooms='" . $Bathrooms . "'";
if($UsageType) $mysqlSelect .= " AND tblListing.UsageType='" . $UsageType . "'";
if($ListingType) $mysqlSelect .= " AND tblListing.ListingType='" . $ListingType . "'";
if($WeekColor) $mysqlSelect .= " AND tblListing.WeekColor='" . $WeekColor . "'";
if($WeeksAvail) $mysqlSelect .= " AND tblListing.WeeksAvail='" . $WeeksAvail . "'";
if($isFloat) $mysqlSelect .= " AND tblListing.isFloat='" . $isFloat . "'";
if($isLocked) $mysqlSelect .= " AND tblListing.isLocked='" . $isLocked . "'";
$mysqlSelect .= "ORDER BY tblResort.City, tblResort.State ASC ";