Forum Moderators: coopster

Message Too Old, No Replies

Search Capabilities in PHP

Want to be able to search multiple fields from multiple tables

         

blunick

8:32 pm on May 11, 2006 (gmt 0)

10+ Year Member



Here is my current dilemma:

I have a form with multiple choices to choose from to find an apartment from our database. When they choose what ever they want on the form it does a GET to my listings search page.

So for each individual field i have for instance

$price = $_GET['price'];
$hood = $_GET['hood'];
$doorman = $_GET['doorman'];

Now i have two table where this info needs to be pulled from an apartments table and a buildings table. So all along i have bee using this query

$query = "SELECT * FROM apts INNER JOIN buildings ON apts.buildid = buildings.buildid AND frontend = 'TRUE'"

to get the two joined together. BUT what im finding is i can't do OR statments with this query I can really only do AND statements. Then im relizing i may have to do 720 different if statements there is no way.

Regardless heres how it looks in the address bar when it does its GET to the listings search page:

/listingsearch2.php?hood=Upper+East+Side&pricelow=1000&pricehigh=3000&doorman=Yes
&elevator=Yes&pets=Yes&outdoor=Yes&beds=1&baths=1&Submit=Search+For+Your+Next+Apartment

So my questions is whats the best way to search two tables for these results in any way the user decides they want to search from the form (i.e Hood & Beds or Hood and Price, or Hood, Price, Bed and Doorman)

THANK YOU AHEAD OF TIME!

[edited by: coopster at 10:22 pm (utc) on May 11, 2006]
[edit reason] broke query string to fix sidescroll [/edit]

coopster

10:28 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, blunick.

I would initialize a WHERE clause and append to it.

$hood = (isset($_GET['hood']))? trim($_GET['hood']) : ''; 
$whereClause = 'WHERE 1=1';
if ($hood) {
$whereClause .= " AND hood = '" . mysql_real_escape_string($hood) . "'";
}

You would follow the same pattern with the rest of the options.

blunick

2:25 am on May 12, 2006 (gmt 0)

10+ Year Member



Coopster thanks a lot...

A WHERE clause I never went down that route, so i would give a where clause for each choice, and check if they are 'isset' and run which ever ones are 'isset' through the query

How would i really structure these where clauses to work properly...

Thank so much for your help!