2

我在许多购物网站上看到他们在侧面有搜索过滤器,您可以添加任意数量的过滤器,它会研究仅显示与所有过滤器查询匹配的数据的数据。

例如,如果您去 ebay 并寻找一台计算机,您可以按计算机的各种规格进行筛选以缩小结果范围。

我遇到的问题是如何为一个包含很多用户可以搜索的字段的表执行此操作。

我有一个属性表,我想通过任意数量的参数(例如租金、位置等)进行搜索。

我可以为每个可能的选项(例如searchByAddress($array)searchByAddressAndRent($array)等)创建搜索查询,但这显然不可行。

另一种方法是我可以为每个字段创建单独的查询,然后为每个参数触发单独的搜索查询,即searchByRent($array)searchByAddress($array)并允许 PHP 应用程序使用array_intersect.

但我想知道,必须有一种适当的技术来实现这一点。这个问题有点啰嗦,我在谷歌上找不到任何关于它的教程。

所以我的问题是,使用各种搜索过滤器搜索数据库表的“正确”方法/技术是什么?

4

1 回答 1

1

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'
        );
}
于 2013-08-27T11:18:23.083 回答