简体   繁体   中英

php/mysql search table with unknown number of filters

I've seen on many shopping websites they have search filters on the side, and you can add any number of filters and it researches the data showing only the data that matches all of the filter queries.

As an example, if you go to ebay and look for a computer, you can filter by various specs of the computer to narrow the results.

The problem I'm having is working out how to do this for a table that has so many fields that a user may search by.

I have a table of Properties that I want to search by any number of parameters eg rent, location, etc.

I could create search queries for each possible option eg searchByAddress($array) , searchByAddressAndRent($array) , etc. but that's clearly not feasible.

Another way I could create separate queries for each field, then trigger separate search queries for each parameter ie searchByRent($array) , searchByAddress($array) and allow the PHP application to compute which fields are common in all resulting arrays using array_intersect .

But I was wondering, there must be a proper technique in achieving this. This question is a bit long-winded and I couldn't find any tutorials on it from googling.

So my question is, what's the "right" method/technique of searching a database table with various search-filters?

If you create a class representing the property table, you can define a static array in the class detailing each field name and corresponding data type.

On the front end, each search field should correspond to the column name in the database, so the $_REQUEST array keys will match the column names.

After this, iterate through your search array, checking each variable exists in your class field definition array, and add it onto the search query.

Below is a very simplified example class which will hopefully give you the idea.

class Property () {

    // search parameters come from $values_array
    // each search field should correspond to the $field_definitions key
    public function search($values_array = null) {

        // populate the values array. Using this method meads you can pass an array directly
        // into the search function, or you can rely on the $_REQUEST
        $values_array = self::getValuesArray($values_array);

        // setup the initial query
        $query = "SELECT * FROM properties";
        // set our initial join string
        $join = "WHERE";

        // loop each of our search values
        foreach ($values_array as $field=>$value) {
            // check the search key exists in our table definition and it's not empty
            if (array_key_exists($field_definitions, self::$fields) && !empty($value)) {
                // switch the datatype for the field so we can set the appropriate query string
                switch (self::$field_definitions[$field]) {

                    case 'int':
                        $query .= "$join $field = {$value} ";
                        $join = "AND";
                        break;

                    default:
                        $query .= "$join $field = '%{$value}%' ";
                        $join = "AND";
                        break;
                }
            }
        }

        // now execute the query... 
        $results = mysql_query($query);

        // do something to process the results and then return

        return $results;
    }

    // basic function to grab our values from $_REQUEST if passed an empty array
    private function getValuesArray($values_array = null) {

        $values_array = (!empty($values_array)) ? $values_array : $_REQUEST;
        return $values_array;
    }

    // variable containing all available fields in table and corresponding datatype
    public static $field_definitions = 
        array(  'number'=>'int',
            'street'=>'string',
            'locality'=>'string',
            'townland'=>'string',
            'town'=>'string',
            'postcode'=>'string'
        );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM